argparse

python中关于传递参数模块argprase的一些小坑

▼魔方 西西 提交于 2019-12-08 17:52:27
今天在写代码的时候遇到了一个关于parser的一些小坑,记录在此备用。 我们知道在python中可以用argprase来传递一些参数给代码执行,来看下面的例子,假设现在有一个test文件夹,下面有3个python文件,分别用a.py;b.py;c.py来表示,目录树如下。 每一个的初始代码为一个简单的print函数。 1 #a.py 2 def out_a(): 3 print("I am a.py") 4 5 6 if __name__ == '__main__': 7 out_a() 1 #b.py 2 def out_b(): 3 print("I am b.py") 4 5 6 if __name__ == '__main__': 7 out_b() 1 #c.py 2 def out_c(): 3 print("I am c.py") 4 5 6 if __name__ == '__main__': 7 out_c() 现在在a.py中引入模块argprase,并定义一些简单的参数,代码如下 1 import argparse 2 parser = argparse.ArgumentParser() 3 parser.add_argument('--first_parameter', default='first') 4 parser.add_argument('-

Python argparse: metavar and action=store_true together

不羁的心 提交于 2019-12-08 15:55:36
问题 I'm using argparse module in Python to parse parameters typed in a command line interface. I have the following add_argument call to a subparser object: submit_parser.add_argument('-pv','--provision',metavar='PROVISION', dest='PROVISION', help='provision system', action='store_true', default=False, required=False) I get this error: Traceback (most recent call last): File "./scripts/tp4", line 94, in <module> main() File "./scripts/tp4", line 74, in main modloader.loadModules(sub_parsers) File

Python - Pass Arguments to Different Methods from Argparse

陌路散爱 提交于 2019-12-08 15:09:57
问题 I'm writing a relatively simple Python script which supports a couple of different commands. The different commands support different options and I want to be able to pass the options parsed by argparse to the correct method for the specified command. The usage string looks like so: usage: script.py [-h] {a, b, c} ... script.py: error: too few arguments I can easily call the appropriate method: def a(): ... def b(): ... def c(): ... if __name__ == "__main__": parser = argparse.ArgumentParser(

argparse accept everything

回眸只為那壹抹淺笑 提交于 2019-12-08 15:02:38
问题 Is there a way to have an argparse.ArgumentParser not raise an exception upon reading an unknown option, but rather put all the unknown options with values in a dictionary, and those without a value in a list? For example, say no arguments were defined in the parser for prog.py , and I pass two arguments: ./prog.py --foo bar --baz I would like the following: parsed = parser.parse_args() vals = parsed.unknown_with_vals novals = parsed.unknown_without_vals print(vals) #{'foo' : 'bar'} print

Python Argparse 'following arguments are required' error

元气小坏坏 提交于 2019-12-08 13:56:04
问题 I'm trying to use a github repo and having some issues with the argparse part of the project. The repo starts like this: import markdown, sys, csv, getpass, smtplib, argparse from email.mime.text import MIMEText from jinja2 import Template parser = argparse.ArgumentParser() parser.add_argument('-m', '--markdown', help='Path to Markdown Template', required=True) parser.add_argument('-c', '--csv', help='Path to CSV file', required=True) parser.add_argument('-v', '--verbose', help='Write out

Syntax error when trying to parse arguments Python shell

空扰寡人 提交于 2019-12-08 11:33:15
问题 I have some code that I am trying to run in a Python shell (IDLE) but there seems to be a problem with the way I am parsing arguments in the Python shell. Here is the code: # import the necessary packages from skimage.segmentation import slic from skimage.segmentation import mark_boundaries from skimage.util import img_as_float from skimage import io import matplotlib.pyplot as plt import argparse # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add

debugging argpars in python

血红的双手。 提交于 2019-12-08 07:19:23
问题 May I know what is the best practice to debug an argpars function. Say I have a py file test_file.py with the following lines # Script start import argparse import os parser = argparse.ArgumentParser() parser.add_argument(“–output_dir”, type=str, default=”/data/xx”) args = parser.parse_args() os.makedirs(args.output_dir) # Script stop The above script can be executed from terminal by: python test_file.py –output_dir data/xx However, for debugging process, I would like to avoid using terminal.

Simple command line application in python - parse user input?

人盡茶涼 提交于 2019-12-08 06:33:40
问题 New to python here - I want to make a command line application where the user will type input I will parse it and execute some command - something in the lines of: try: while True: input = raw_input('> ') # parse here except KeyboardInterrupt: pass The user is supposed to type commands like init /path/to/dir . Can I use argparse to parse those ? Is my way too crude ? 回答1: arparse is a perfect solution for what you propose. The docs are well written and show dozens of example of how to invoke

Using positional arguments from argparse as function name

可紊 提交于 2019-12-08 06:26:21
问题 I have set up a script with argparse that gives me the following NameSpace: Namespace(action='list', input='all', target='domain') I have made a few functions which are called according to the positionals, and at the moment I have a working situation by calling them with blurbs of code like this one: if args.action == 'list': if len(sys.argv) == 2: parser.print_help() sys.exit(0) elif args.target == 'domain': domain_list() elif args.target == 'forwarding': forwarding_list() elif args.target =

Python: read command line args with argparse in addition to those coming from an external function

岁酱吖の 提交于 2019-12-08 06:15:38
问题 I have two modules in the same package, module1.py and module2.py . In module1.py I have a function reading command-line args with argparse : import argparse def parse_arguments_module1(): parser = argparse.ArgumentParser() optional = parser._action_groups.pop() required = parser.add_argument_group('required arguments') required.add_argument('--argA', help='argA', required=True) required.add_argument('--argB', help='argB', required=True) optional.add_argument('--argC', help='argC') optional