在python2.7版本及以上有这个内库
示例:
p = argparse.ArgumentParser(prog="mssh",formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=epilog,conflict_handler="resolve",description="run command on target hosts simultaneously over ssh")
#可以创建组,然后组下面可以再添加新的参数
g = p.add_mutually_exclusive_group()
g.add_argument("-h", "--host", dest="hostname", type=str,
help="hostname or ip of target hosts")
g.add_argument("-f", "--file", dest="hostfile", type=str,
help="file contains hostname or ip of target hosts")
p.add_argument(dest="command", type=str, nargs="+",
help="command to be execute on target host")
options = p.parse_args()
print options.command
第一步:创建一个解析器
parser = argparse.ArgumentParser(description='Process some integers.')
第二步:往解析器里面填充参数
>>> 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)')
最后一步:调用parse_args返回两个属性integers and accumulate
class argparse.ArgumentParser([description][, epilog][, prog][, usage][, add_help][, argument_default][, parents][, prefix_chars][, conflict_handler][, formatter_class])
* 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)
ArgumentParser.add_argument(name or flags...[, action][, nargs][, const][, default][, type][, choices][, required][, help][, metavar][, dest])
* 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().
阅读(6941) | 评论(0) | 转发(0) |