argparse

Multiple files for one argument in argparse Python 2.7

╄→尐↘猪︶ㄣ 提交于 2019-12-12 07:10:01
问题 Trying to make an argument in argparse where one can input several file names that can be read. In this example, i'm just trying to print each of the file objects to make sure it's working correctly but I get the error: error: unrecognized arguments: f2.txt f3.txt . How can I get it to recognize all of them? my command in the terminal to run a program and read multiple files python program.py f1.txt f2.txt f3.txt Python script import argparse def main(): parser = argparse.ArgumentParser()

Not getting Namespace returned from subparser in parse_args()

寵の児 提交于 2019-12-12 05:50:01
问题 I can't seem to figure out how to call the add_user() function in the script and pass it all the arguments needed. If I do this if args.adduser: add_user(username, account, groups) it doesn't find the adduser Namespace . AttributeError: 'Namespace' object has no attribute 'adduser' However, if I add this parser_adduser.set_defaults(func=add_user) it appears to execute the add_user() function, but doesn't pass it any of the arguments. def parse_args(): helptext = 'Script to Add, Delete, Update

Support arbitrary number of related named arguments with Python argparse

狂风中的少年 提交于 2019-12-12 05:14:34
问题 I'd like to support a command line interface where users can declare an arbitrary number of samples, with one or more input files corresponding to each sample. Something like this: $ myprogram.py \ --foo bar \ --sample1 input1.tsv \ --sample2 input2a.tsv input2b.tsv input2c.tsv \ --sample3 input3-filtered.tsv \ --out output.tsv The idea is that the option keys will match the pattern --sample(\d+) , and each key will consume all subsequent arguments as option values until the next - or --

python argparse different parameters with different number of arguments

╄→尐↘猪︶ㄣ 提交于 2019-12-12 04:58:16
问题 How do I use a different number of parameters for each option? ex) a.py parser.add_argument('--opt', type=str,choices=['a', 'b', 'c'],help='blah~~~') choice : a / parameter : 1 ex) $ python a.py --opt a param choice : c / parameter :2 ex) $ python a.py --opt b param1 param2 回答1: You need to add sub-commands, ArgumentParser.add_subparsers() method will help you Check this example >>> # create the top-level parser >>> parser = argparse.ArgumentParser(prog='PROG') >>> parser.add_argument('--foo'

Python argparse override help from parent

≡放荡痞女 提交于 2019-12-12 04:39:16
问题 I have a CLI I'm building which utilizes subparsers for sub-commands similar to tools like git. Some of my sub-commands share common options so I have a group parser that defines the options, and each sub-command that needs them uses parents=group_parser as one of the arguments. For example: group_parser = argparse.ArgumentParser() group_parser.add_argument('-f', '--foo', action='store_true') command1_parser = subparsers.add_parser('command1', parents=[group_parser]) command2_parser =

Converting a Python argparse CLI program into a GUI with Tkinter?

≯℡__Kan透↙ 提交于 2019-12-12 04:34:05
问题 I have a simple CLI based program that I would like to add a GUI to. Optimally I would like to retain the ability to have this script run via the CLI as well. If this can be done, what is the best way to approach this? Disclaimer: I am relatively new to Tkinter! from argparse import ArgumentParser from ipaddress import IPv4Network def Main(): """ Main Program """ parser = ArgumentParser( description='Provided a list of IP addresses, format and output the correct fortigate commands to create

arparse output not aligned

橙三吉。 提交于 2019-12-12 04:13:30
问题 I am using argparse for arguments, I have numbers of argparse statements. I want in the output the capital DELETE should not be print or they should be aligned. In my case for another argparse the capital words are not aligned in a single column. parser = argparse.ArgumentParser() parser.add_argument( '-del' ,action='store' ,dest='delete' , help="Del a POX" parser.add_argument( '-a' ,action='store' ,dest='add' , help="add a POX" return parser python myscript.h -h -del DELETE Del a POX -a Add

Python 3 gettext not working for argparse

心已入冬 提交于 2019-12-12 03:57:22
问题 I want to translate 'usage: ' into 'foobar'. print(gettext.find('test', 'i18n', ['en_US'])) translator = gettext.translation('test', localedir='i18n', languages=['en_US']) translator.install() print(_('usage: ')) parser = argparse.ArgumentParser(prog="jcheq.py", usage="%(prog)s [opciones] [paths...]\nThe paths are optional; if not given . is " "used.", add_help=False, formatter_class=argparse.ArgumentDefaultsHelpFormatter) But it outputs this: ~/Desktop/Proyectos/UNPAZ/jcheq/jcheq$ python3

How can I test whether my code is throwing the appropriate argparse exceptions?

孤者浪人 提交于 2019-12-12 03:55:18
问题 From this great answer I learned to put argument parsing into its own function to simplify unit testing. From this answer I learned that sometimes you need to throw your own parser errors to get argparse to perform the behaviour you want. E.g.: if not (args.process or args.upload): parser.error('No action requested, add -process or -upload') But it is hard to test if this does what it should since throwing the parser error also exits the program. So something like this TestCase won't work:

Can't get argparse to read quoted string with dashes in it?

独自空忆成欢 提交于 2019-12-12 03:34:41
问题 Is there a way to make argparse recognize anything between two quotes as a single argument? It seems to keep seeing the dashes and assuming that it's the start of a new option I have something like: mainparser = argparse.ArgumentParser() subparsers = mainparser.add_subparsers(dest='subcommand') parser = subparsers.add_parser('queue') parser.add_argument('-env', '--extraEnvVars', type=str, help='String of extra arguments to be passed to model.') ...other arguments added to parser... But when I