argparse

Print program usage example with argparse module

半世苍凉 提交于 2019-12-03 01:01:43
I am trying to learn how to use python's argparse module. Currently my python script is: parser = argparse.ArgumentParser(description='My first argparse attempt', add_help=True) parser.add_argument("-q", action ="store", dest='argument', help="First argument") output = parser.parse_args() And it gives the output as : usage: test.py [-h] [-q ARGUMENT] My first argparse attempt optional arguments: -h, --help show this help message and exit -q ARGUMENT First argument Now, lets suppose I want my -h or --help argument to print a usage example also. Like, Usage: python test.py -q "First Argument for

argparse module How to add option without any argument?

狂风中的少年 提交于 2019-12-03 00:55:58
问题 I have created a script using argparse . The script needs to take a configuration file name as an option, and user can specify whether they need to proceed totally the script or only simulate it. The args to be passed: ./script -f config_file -s or ./script -f config_file . It's ok for the -f config_file part, but It keeps asking me for arguments for the -s which is optionnal and should not be followed by any. I have tried this: parser = argparse.ArgumentParser() parser.add_argument('-f', '-

What does metavar and action mean in argparse in Python?

房东的猫 提交于 2019-12-03 00:54:49
I am reading through argparse module. I got stuck as what to metavar and action means >>> parser.add_argument('integers', metavar='N', type=int, nargs='+', ... help='an integer for the accumulator') >>> parser.add_argument('--sum', dest='accumulate', action='store_const', ... const=sum, default=max, ... help='sum the integers (default: find the max)') I might have missed but from what I read, I could not find definitions for metavar and action (action="store_const", etc) . what do they actually mean? metavar is used in help messages in a place of an expected argument. See FOO is a default

argparse简要用法总结

匿名 (未验证) 提交于 2019-12-03 00:11:01
转: http://vra.github.io/2017/12/02/argparse-usage/ argparse 是python自带的命令行参数解析包,可以用来方便地读取命令行参数,当你的代码需要频繁地修改参数的时候,使用这个工具可以将参数和代码分离开来,让你的代码更简洁,适用范围更广。 argparse使用比较简单,常用的功能可能较快地实现出来,下面我分几个步骤, 以Python3为例 ,逐渐递增地讲述argparse的用法。 下面是使用argparser从命令行获取用户名,然后打印’Hello ‘+ 用户名,假设python文件名为 print_name.py : # file-name:print_name.pyimport argparsedef get_parser(): parser = argparse.ArgumentParser(description="Demo of argparse") parser.add_argument('--name', default='Great') return parserif __name__ == '__main__': parser = get_parser() args = parser.parse_args() name = args.name print('Hello {}'.format(name))

关于argparse

匿名 (未验证) 提交于 2019-12-03 00:09:02
作用就是用来为脚本添加命令行参数,例如下面的代码: # coding:utf-8 from __future__ import absolute_import import argparse import os import logging from src . tfrecord import main def parse_args (): parser = argparse . ArgumentParser () parser . add_argument ( '-t' , '--tensorflow-data-dir' , default = 'pic/' ) parser . add_argument ( '--train-shards' , default = 2 , type = int ) parser . add_argument ( '--validation-shards' , default = 2 , type = int ) parser . add_argument ( '--num-threads' , default = 2 , type = int ) parser . add_argument ( '--dataset-name' , default = 'satellite' , type = str ) return parser .

Multiple positional arguments with Python and argparse

╄→гoц情女王★ 提交于 2019-12-02 23:47:12
I'm trying to use argparse to parse the command line arguments for a program I'm working on. Essentially, I need to support multiple positional arguments spread within the optional arguments, but cannot get argparse to work in this situation. In the actual program, I'm using a custom action (I need to store a snapshot of the namespace each time a positional argument is found), but the problem I'm having can be replicated with the append action: >>> import argparse >>> parser = argparse.ArgumentParser() >>> parser.add_argument('-a', action='store_true') >>> parser.add_argument('-b', action=

Python(2)-argparse模块常用模式

匿名 (未验证) 提交于 2019-12-02 22:51:30
argparse 1基本函数 2例子程序演示 3常用参数解释 4argparse模块整理的缘起 1基本函数 argparse是Python中用于命令行中进行参数解析的一个模块,可以自动生成help和usage信息;当从终端输入的参数无效时,模块会输出提示信息。 Argparse常用的三个函数: parser=Argparse.ArgumentParser() ArgumentParser()用于创建一个ArgumentParser对象parser,parser保存了所有必要信息,用于将“从命令行中读入的参数”解析为对应的python数据类型。 parser.add_argument() 用于给parser添加需要读取数据的信息,这些信息告诉parser解析读入参数的方法 args = parser.parse_args() 用于解析parser保存的参数,返回一个命名空间 在实际python脚本中parse_args()一般不使用参数,它的参数由sys.argv确定。 2例子程序演示 将下列代码存成.py文件,在终端中运行。 import argparse parser=argparse.ArgumentParser(description="process some integer.") parser.add_argument('integers',metavar='N'

How to make an optional value for argument using argparse?

依然范特西╮ 提交于 2019-12-02 21:43:32
I am creating a python script where I want to have an argument that manipulates how many search results you get as output. I've currently named the argument --head . This is the functionality I'd like it to have: When --head is not passed at the command line I'd like it to default to one value. In this case, a rather big one, like 80 When --head is passed without any value, I'd like it to default to another value. In this case, something limited, like 10 When --head is passed with a value, I'd like it to store the value it was passed. Here is some code describing the problem: >>> import

Python does not state the line of code that an error refers to

江枫思渺然 提交于 2019-12-02 18:15:26
问题 I have tried to correct the following error which basically is about the arguments for 'username' and 'directory'. I have tried all possible ways but no luck. Python does not state the line of code that the following error refers to: usage: Google_Map.py [-h] [-n NUM_TO_DOWNLOAD] [-l LOG_LEVEL] username directory Google_Map.py: error: the following arguments are required: username, directory Please see the code here: def __init__(self, username, directory, num_to_download = 10, log_level=