博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
argparse–Parser for command-line options
阅读量:6511 次
发布时间:2019-06-24

本文共 3031 字,大约阅读时间需要 10 分钟。

1. Example

import argparse parser = argparse.ArugumentParser() parser.add_argument('-s', '--long', type=int, nargs='?', const = 12, default = 12) settings = parser.parse_args() print (settings.long)

2. ArgumentParser objects

class argparse.ArgumentParser([description][, epilog][, prog][, usage][, add_help][, argument_default][, parents][, prefix_chars][, conflict_handler][, formatter_class])

Create a new ArgumentParser object. Each parameter has its own more detailed description below, but in short they are:

  • description - Text to display before the argument help.
  • epilog - Text to display after the argument help.
  • add_help - Add a -h/–help option to the parser. (default: True)
  • argument_default - Set the global default value for arguments. (default: None)
  • parents - A list of ArgumentParser objects whose arguments should also be included.
  • prefix_chars - The set of characters that prefix optional arguments. (default: ‘-‘)
  • fromfile_prefix_chars - The set of characters that prefix files from which additional arguments should be read. (default: None)
  • formatter_class - A class for customizing the help output.
  • conflict_handler - Usually unnecessary, defines strategy for resolving conflicting optionals.
  • prog - The name of the program (default: sys.argv[0])
  • usage - The string describing the program usage (default: generated)

3. add_argument() method

ArgumentParser.
add_argument
(
name or flags...[,
action][,
nargs][,
const][,
default][,
type][,
choices][,
required][,
help][,
metavar][,
dest]
)

Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are:

  • name or flags - Either a name or a list of option strings, e.g. foo or -f, --foo.
  • action - The basic type of action to be taken when this argument is encountered at the command line.
  • nargs - The number of command-line arguments that should be consumed.
  • const - A constant value required by some action and nargs selections.
  • default - The value produced if the argument is absent from the command line.
  • type - The type to which the command-line argument should be converted.
  • choices - A container of the allowable values for the argument.
  • required - Whether or not the command-line option may be omitted (optionals only).
  • help - A brief description of what the argument does.
  • metavar - A name for the argument in usage messages.
  • dest - The name of the attribute to be added to the object returned by parse_args().

4. parse_args() method

ArgumentParser.
parse_args
(
args=None,
namespace=None
)

Convert argument strings to objects and assign them as attributes of the namespace. Return the populated namespace.

Previous calls to add_argument() determine exactly what objects are created and how they are assigned. See the documentation for add_argument() for details.

By default, the arg strings are taken from sys.argv, and a new empty Namespace object is created for the attributes.

5. Sub-commands

to be continued.

转载于:https://www.cnblogs.com/kongs/archive/2011/11/17/2252522.html

你可能感兴趣的文章