argparse

Python how to get value from argparse from variable, but not the name of the variable?

ε祈祈猫儿з 提交于 2021-02-20 13:30:39
问题 If I do args.svc_name, I was expecting equivalent to args.option1, because the svc_name's value is option1. But i got error for "'Namespace' object has no attribute 'svc_name's'" parser = argparse.ArgumentParser(prog=None,description="test") parser.add_argument("--option1", nargs="?",help="option") args = parser.parse_args() svc_name = "option1" print (args.svc_name) Can someone help me out? 回答1: The ArgumentParser class exposes the keys as direct attributes rather than a mapping, so to get

Permit argparse global flags after subcommand

只谈情不闲聊 提交于 2021-02-18 10:14:34
问题 I am using argparse to build a command with subcommands: mycommand [GLOBAL FLAGS] subcommand [FLAGS] I would like the global flags to work whether they are before or after the subcommand. Is there a clean way to do this that doesn't involve repeating code? For example: parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subparser_name') parser.add_argument('--disable') # This flag... sp = subparsers.add_parser('compile') sp.add_argument('zones', nargs='*') sp.add

Permit argparse global flags after subcommand

柔情痞子 提交于 2021-02-18 10:12:52
问题 I am using argparse to build a command with subcommands: mycommand [GLOBAL FLAGS] subcommand [FLAGS] I would like the global flags to work whether they are before or after the subcommand. Is there a clean way to do this that doesn't involve repeating code? For example: parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subparser_name') parser.add_argument('--disable') # This flag... sp = subparsers.add_parser('compile') sp.add_argument('zones', nargs='*') sp.add

Permit argparse global flags after subcommand

為{幸葍}努か 提交于 2021-02-18 10:12:18
问题 I am using argparse to build a command with subcommands: mycommand [GLOBAL FLAGS] subcommand [FLAGS] I would like the global flags to work whether they are before or after the subcommand. Is there a clean way to do this that doesn't involve repeating code? For example: parser = argparse.ArgumentParser() subparsers = parser.add_subparsers(dest='subparser_name') parser.add_argument('--disable') # This flag... sp = subparsers.add_parser('compile') sp.add_argument('zones', nargs='*') sp.add

利用 Python 批量修改文件名

北城余情 提交于 2021-02-17 10:59:41
开发的第一步,首先得熟悉脚本中使用的模块函数,废话不多说,开干! 1 函数介绍 1.1 os 模块 (1)os.lisdir() >>> import os >>> print(os.listdir()) ['.env', 'rename.py', '.rename.py.swp'] 此函数的作用即列出指定目录下的所有文件与目录的名,并以列表形式展现。 (2)os.path.splitext() >>> import os >>> print(os.path.splitext('rename.py')) ('rename', '.py') 在函数中指定一个文件名,用于分割文件名与后缀,结果存储于元组中 (3)os.path.join() >>> import os >>> print(os.path.join('/etc','nginx','nginx.conf')) /etc/nginx/nginx.conf 将函数中的目录、文件组合成一个路径 (4)os.rename() >>> import os >>> os.rename('src','dest') # src 为原文件名,dest 为目标文件名 将目录或文件重命名 1.2 argparse 模块 ​ Python 中内置模块,用于解析给出的函数参数,生成有用信息。 # argparse 模块示例源码 [root

How to implement argparse in Python

南楼画角 提交于 2021-02-11 15:41:28
问题 I'm new to Python, I got a small script to upload files to S3, at the moment I only hard-code one single file in the script, the bucket name is also hard-coded. I wanted to merge argparse in this script so that I can add some arguements by myself and upload different files. For example, in the command line I can specify arguments to decide file_name x upload to bucket_name xxx . I've been reading documents about how to set argparse but I can only make small changes and don't know how to

Argparse optional arguments with multilevel parser/subparser

你说的曾经没有我的故事 提交于 2021-02-11 15:40:37
问题 I have a set of parsers and subparsers to build a production or development system. If the user picks production, he can add options and all is well. If he pics development, he can enter an architecture and then enter build options. This is where it gets sticky. I want him to be able to select build option 'comms' 'server' or 'all', but if he picks server, he has more choices. My implementation is below. I tried combinations of parsers and subpasers (it seems that arguments can only be added

Python argparse error: error: argument count: invalid int value

ぃ、小莉子 提交于 2021-02-10 14:26:49
问题 I am trying to run below code in jupyter notebook. import argparse parser = argparse.ArgumentParser(description='Example with non-optional arguments') parser.add_argument('count', action="store", type=int) parser.add_argument('units', action="store") print(parser.parse_args()) but this gives this gives below error usage: ipykernel_launcher.py [-h] count units ipykernel_launcher.py: error: argument count: invalid int value: 'C:\\Users\\Kwan Lee\\AppData\\Roaming\\jupyter\\runtime\\kernel

argparse: Ignore positional arguments if a flag is set?

蹲街弑〆低调 提交于 2021-02-08 12:01:34
问题 I want to provide the command with 3 arguments: <version> , <input file> and <output file> under normal usage. Except there's a specific flag --init , which will basically run the program without any input and output file specifications required. I would therefore ideally have a command which usage is: py program.py --init OR py program.py <version> <input file> <output file> However, since positional arguments are always required (because all 3 are required in any other circumstance than -

Support global arguments before or after sub-command in argparse

给你一囗甜甜゛ 提交于 2021-02-05 09:37:19
问题 Setting the parents argument with a parser will allow for sharing common arguments between parsers (e.g. parents and sub-commands). But, applying a base parser to both the parent and sub-command appears to overwrite the value from the parent parser with the value from the sub-command parser when using an argument that has specified the value attribute to with a dest keyword, whether or not the invocation has specified the argument in the sub-command. How can I use argparse module to merge the