argparse

Display help message with python argparse when script is called without any arguments

柔情痞子 提交于 2019-11-26 10:07:52
问题 This might be a simple one. Assume I have a program that uses argparse to process command line arguments/options. The following will print the \'help\' message: ./myprogram -h or: ./myprogram --help But, if I run the script without any arguments whatsoever, it doesn\'t do anything. What I want it to do is to display the usage message when it is called with no arguments. How is that done? 回答1: This answer comes from Steven Bethard on Google groups. I'm reposting it here to make it easier for

Argparse with required subparser

廉价感情. 提交于 2019-11-26 09:35:20
问题 I\'m using Python 3.4, I\'m trying to use argparse with subparsers, and I want to have a similar behavior to the one in Python 2.x where if I don\'t supply a positional argument (to indicate the subparser/subprogram) I\'ll get a helpful error message. I.e., with python2 I\'ll get the following error message: $ python2 subparser_test.py usage: subparser_test.py [-h] {foo} ... subparser_test.py: error: too few arguments I\'m setting the required attribute as suggested in https://stackoverflow

Python argparse: How to insert newline in the help text?

泪湿孤枕 提交于 2019-11-26 06:12:30
问题 I\'m using argparse in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g. from argparse import ArgumentParser parser = ArgumentParser(description=\'test\') parser.add_argument(\'-g\', choices=[\'a\', \'b\', \'g\', \'d\', \'e\'], default=\'a\', help=\"Some option, where\\n\" \" a = alpha\\n\" \" b = beta\\n\" \" g = gamma\\n\" \" d = delta\\n\" \" e = epsilon\") parser.parse_args() However, argparse strips all newlines and

How can I pass a list as a command-line argument with argparse?

家住魔仙堡 提交于 2019-11-26 05:51:37
问题 I am trying to pass a list as an argument to a command line program. Is there an argparse option to pass a list as option? parser.add_argument(\'-l\', \'--list\', type=list, action=\'store\', dest=\'list\', help=\'<Required> Set flag\', required=True) Script is called like below python test.py -l \"265340 268738 270774 270817\" 回答1: TL;DR Use the nargs option or the 'append' setting of the action option (depending on how you want the user interface to behave). nargs parser.add_argument('-l','

Why use argparse rather than optparse?

依然范特西╮ 提交于 2019-11-26 04:32:02
问题 I noticed that the Python 2.7 documentation includes yet another command-line parsing module. In addition to getopt and optparse we now have argparse . Why has yet another command-line parsing module been created? Why should I use it instead of optparse ? Are there new features that I should know about? 回答1: As of python 2.7 , optparse is deprecated, and will hopefully go away in the future. argparse is better for all the reasons listed on its original page (https://code.google.com/archive/p

Argparse optional positional arguments?

旧街凉风 提交于 2019-11-26 04:05:27
问题 I have a script which is meant to be used like this: usage: installer.py dir [-h] [-v] dir is a positional argument which is defined like this: parser.add_argument(\'dir\', default=os.getcwd()) I want the dir to be optional: when it\'s not specified it should just be cwd . Unfortunately when I don\'t specify the dir argument, I get Error: Too few arguments . 回答1: Use nargs='?' (or nargs='*' if you will need more than one dir) parser.add_argument('dir', nargs='?', default=os.getcwd()) extended

Can&#39;t get argparse to read quoted string with dashes in it?

南楼画角 提交于 2019-11-26 03:58:03
问题 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...

Simple argparse example wanted: 1 argument, 3 results

霸气de小男生 提交于 2019-11-26 03:31:16
问题 The documentation for the argparse python module, while excellent I\'m sure, is too much for my tiny beginner brain to grasp right now. I don\'t need to do math on the command line or meddle with formatting lines on the screen or change option characters. All I want to do is \"If arg is A, do this, if B do that, if none of the above show help and quit\" . 回答1: My understanding of the original question is two-fold. First, in terms of the simplest possible argparse example, I'm surprised that I

Parsing boolean values with argparse

荒凉一梦 提交于 2019-11-26 01:55:46
问题 I would like to use argparse to parse boolean command-line arguments written as \"--foo True\" or \"--foo False\". For example: my_program --my_boolean_flag False However, the following test code does not do what I would like: import argparse parser = argparse.ArgumentParser(description=\"My parser\") parser.add_argument(\"--my_bool\", type=bool) cmd_line = [\"--my_bool\", \"False\"] parsed_args = parser.parse(cmd_line) Sadly, parsed_args.my_bool evaluates to True . This is the case even when