python3 当命令行参数为 TRUE 时执行函数
python3 Execute a function when command line arg is TRUE
我有两个函数,(1) 运行进行演示分析和 (2) 通过文件读取的自定义函数。我想将用户的命令行参数传递给 select 演示函数或自定义函数为 true 或 false。不需要传递其他值。我不确定我应该在函数中添加什么来接受 arg.parse
def demo()
print("This is demo function")
def custom()
print("This is custom function")
def main():
parser = argparse.ArgumentParser(description="Argument Parser is boolean to run Pipeline'")
parser.add_argument("--demo", help='Demo data to show an example')
parser.add_argument("--custom", help='Specify custom analysis, sql query is read from userquery.sql')
args = parser.parse_args()
if args.demo == True:
demoanalysis()
elif args.custom == True:
customanalysis()
else:
print("Don't do anything")
然而,当我 运行 脚本时,它直接进入第三个选项。
python script.py --demo True --custom False
Don't do anything
我知道需要向每个函数传递一些东西来接受布尔参数,但不确定如何去做。有什么建议么。谢谢。
我猜,你可以使用 action="store_true"
参数(更多在 docs 中):
def main():
parser = argparse.ArgumentParser(description="Argument Parser is boolean to run Pipeline'")
parser.add_argument("--demo", action="store_true", help='Demo data to show an example')
parser.add_argument("--custom", action="store_true", help='Specify custom analysis, sql query is read from userquery.sql')
args = parser.parse_args()
if args.demo: # need no comparison with True, because demo is True or False itself
demoanalysis()
elif args.custom:
customanalysis()
else:
print("Don't do anything")
Edit:如果命令行参数中没有关键字,store_true
默认创建 False
值。
使用示例:
python demo_script.py --demo # prints "This is demo function"
python demo_script.py --custom # prints "This is custom function"
希望对您有所帮助!
您还可以构建一个函数来检查您传递给函数的参数是否有效:
def main():
parser = argparse.ArgumentParser(description="Argument Parser is boolean to run Pipeline'")
# Here you add a type definition to your arguments
parser.register('type', 'bool', (lambda x: str(x).lower() == "true") )
parser.add_argument("--demo", type="bool", help='Demo data to show an example')
parser.add_argument("--custom", type="bool", help='Specify custom analysis, sql query is read from userquery.sql')
args = parser.parse_args()
if args.demo:
demoanalysis()
elif args.custom:
customanalysis()
else:
print("Don't do anything")
我有两个函数,(1) 运行进行演示分析和 (2) 通过文件读取的自定义函数。我想将用户的命令行参数传递给 select 演示函数或自定义函数为 true 或 false。不需要传递其他值。我不确定我应该在函数中添加什么来接受 arg.parse
def demo()
print("This is demo function")
def custom()
print("This is custom function")
def main():
parser = argparse.ArgumentParser(description="Argument Parser is boolean to run Pipeline'")
parser.add_argument("--demo", help='Demo data to show an example')
parser.add_argument("--custom", help='Specify custom analysis, sql query is read from userquery.sql')
args = parser.parse_args()
if args.demo == True:
demoanalysis()
elif args.custom == True:
customanalysis()
else:
print("Don't do anything")
然而,当我 运行 脚本时,它直接进入第三个选项。
python script.py --demo True --custom False
Don't do anything
我知道需要向每个函数传递一些东西来接受布尔参数,但不确定如何去做。有什么建议么。谢谢。
我猜,你可以使用 action="store_true"
参数(更多在 docs 中):
def main():
parser = argparse.ArgumentParser(description="Argument Parser is boolean to run Pipeline'")
parser.add_argument("--demo", action="store_true", help='Demo data to show an example')
parser.add_argument("--custom", action="store_true", help='Specify custom analysis, sql query is read from userquery.sql')
args = parser.parse_args()
if args.demo: # need no comparison with True, because demo is True or False itself
demoanalysis()
elif args.custom:
customanalysis()
else:
print("Don't do anything")
Edit:如果命令行参数中没有关键字,store_true
默认创建 False
值。
使用示例:
python demo_script.py --demo # prints "This is demo function"
python demo_script.py --custom # prints "This is custom function"
希望对您有所帮助!
您还可以构建一个函数来检查您传递给函数的参数是否有效:
def main():
parser = argparse.ArgumentParser(description="Argument Parser is boolean to run Pipeline'")
# Here you add a type definition to your arguments
parser.register('type', 'bool', (lambda x: str(x).lower() == "true") )
parser.add_argument("--demo", type="bool", help='Demo data to show an example')
parser.add_argument("--custom", type="bool", help='Specify custom analysis, sql query is read from userquery.sql')
args = parser.parse_args()
if args.demo:
demoanalysis()
elif args.custom:
customanalysis()
else:
print("Don't do anything")