如何在命令行中创建用于获取输入字符串的自定义标记,并将它们存储在Python中的变量中
获取输入的格式为:如何在命令行中创建用于获取输入字符串的自定义标记,并将它们存储在Python中的变量中,python,python-3.x,command,command-line-interface,Python,Python 3.x,Command,Command Line Interface,获取输入的格式为: myprogram.py -f "string1" -t "string2" -i "string 3 some directory path" 您应该使用argparsePython模块来解析CLI参数。我给你写了一个例子 argparse的官方文档: 代码: from argparse import ArgumentParser parser = ArgumentParser(description=__do
myprogram.py -f "string1" -t "string2" -i "string 3 some directory path"
您应该使用
argparse
Python模块来解析CLI参数。我给你写了一个例子
argparse
的官方文档:
代码:
from argparse import ArgumentParser
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"-f",
dest="first_string",
help="Your first string parameter",
required=True,
)
parser.add_argument(
"-t",
dest="second_string",
help="Your second string parameter",
required=True,
)
parser.add_argument(
"-i",
dest="third_string",
help="Your third string parameter",
required=True,
)
input_parameters, unknown_input_parameters = parser.parse_known_args()
# Set CLI argument variables.
first_arg = input_parameters.first_string
second_arg = input_parameters.second_string
third_arg = input_parameters.third_string
print("-f: {}\n"
"-t: {}\n"
"-i: {}".format(first_arg, second_arg, third_arg))
>>> python3 test.py -f "string1" -t "string2" -i "string 3 some directory path"
-f: string1
-t: string2
-i: string 3 some directory path
>>> python3 test.py -t "string1" -f "string2" -i "string 3 some directory path"
-f: string2
-t: string1
-i: string 3 some directory path
>>> python3 test.py -t "string1" -f "string2" -i "string 3 some directory path" -x "Unused"
-f: string2
-t: string1
-i: string 3 some directory path
>>> python3 test.py -t "string1" -i "string 3 some directory path"
usage: test.py [-h] -f FIRST_STRING -t SECOND_STRING -i THIRD_STRING
test.py: error: the following arguments are required: -f
>>> python3 test.py -h
usage: test.py [-h] -f FIRST_STRING -t SECOND_STRING -i THIRD_STRING
optional arguments:
-h, --help show this help message and exit
-f FIRST_STRING Your first string parameter
-t SECOND_STRING Your second string parameter
-i THIRD_STRING Your third string parameter
一些输出:
from argparse import ArgumentParser
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"-f",
dest="first_string",
help="Your first string parameter",
required=True,
)
parser.add_argument(
"-t",
dest="second_string",
help="Your second string parameter",
required=True,
)
parser.add_argument(
"-i",
dest="third_string",
help="Your third string parameter",
required=True,
)
input_parameters, unknown_input_parameters = parser.parse_known_args()
# Set CLI argument variables.
first_arg = input_parameters.first_string
second_arg = input_parameters.second_string
third_arg = input_parameters.third_string
print("-f: {}\n"
"-t: {}\n"
"-i: {}".format(first_arg, second_arg, third_arg))
>>> python3 test.py -f "string1" -t "string2" -i "string 3 some directory path"
-f: string1
-t: string2
-i: string 3 some directory path
>>> python3 test.py -t "string1" -f "string2" -i "string 3 some directory path"
-f: string2
-t: string1
-i: string 3 some directory path
>>> python3 test.py -t "string1" -f "string2" -i "string 3 some directory path" -x "Unused"
-f: string2
-t: string1
-i: string 3 some directory path
>>> python3 test.py -t "string1" -i "string 3 some directory path"
usage: test.py [-h] -f FIRST_STRING -t SECOND_STRING -i THIRD_STRING
test.py: error: the following arguments are required: -f
>>> python3 test.py -h
usage: test.py [-h] -f FIRST_STRING -t SECOND_STRING -i THIRD_STRING
optional arguments:
-h, --help show this help message and exit
-f FIRST_STRING Your first string parameter
-t SECOND_STRING Your second string parameter
-i THIRD_STRING Your third string parameter
这回答了你的问题吗?您好,欢迎使用stackoverflow,请提供您的程序代码。欢迎使用!如果我的回答解决了你的问题,请接受我的回答!:)