simpleopt-命令行选项的简单解析器

simpleopt的Python项目详细描述


用法示例

您只需定义一个函数,并有选择地注释 参数,相同的参数将在命令行上解析:

@annotation(command=str, interval=float, differences=bool,
            args=str, kwargs=str)
def watch(command, interval=2.0, differences=False, *args, **kwargs):
    print (command, interval, differences, args, kwargs)

if __name__ == '__main__':
    SimpleOpt(watch).run()

如果你把它保存在一个名为 watch.py并使其可执行:

$ watch.py "df -h"
('df -h', 2.0, False, (), {})

$ watch.py --interval 1.0 "df -h"
('df -h', 1.0, False, (), {})

$ watch.py --noverbose --differences "df -h"
('df -h', 1.0, True, (), {})

$ watch.py --foo bar --bar baz "df -h" quux quuux
('df -h', 2.0, False, ('quux', 'quuux'), {'foo': 'bar', 'bar': 'baz'})

另一个例子:

@annotation(foo={str:int})
def example(foo):
    return foo

$ example.py --foo a:1,b:2,c:3
{'a': 1, 'c': 3, 'b': 2}

$ example.py a:1,b:2,c:3
{'a': 1, 'c': 3, 'b': 2}

工作细节

命令行参数分为以下两种类型之一:(i)选项 参数和(ii)位置参数。

  1. option arguments have the form –OPTION or –OPTION=VALUE where OPTION is the argument name and VALUE is an optional value given to that argument.
  2. positional arguments are those that are not option arguments.

命令行参数分配给函数的形式 参数不同于python在python中分配输入参数的方式 代码。

在命令行上运行python脚本时,命令行参数是 分配给函数的形式参数如下:

  • For each formal parameter, there is a slot which will be used to contain the value of the argument assigned to that parameter.

  • Each slot is either ‘empty’ or ‘filled’. Slots which had values assigned to them are ‘filled’, otherwise they are ‘empty’.

  • Initially, all slots are marked ‘empty’.

  • Option arguments are assigned first, followed by positional arguments.

  • For each option argument:

    o If there is a parameter with the same name as the option argument, then

    the argument value is assigned to that parameter slot. However, if the parameter is already filled, then that is an error.

    o Otherwise, if there is a ‘keyword dictionary’ argument, the argument is

    added to the dictionary using the keyword name as the dictionary key, unless there is already an entry with that key, in which case it is an error.

    o Otherwise, if there is no keyword dictionary, and no matching named

    parameter, then it is an error.

  • 对于每个位置参数:

    o Attempt to bind the argument to the first unfilled parameter slot that

    has no default value. If the slot is not a vararg slot, then mark the slot as ‘filled’.

    o Otherwise, if the next unfilled slot is a vararg slot then all remaining

    positional arguments are placed into the vararg slot.

  • 最后:

    o If the vararg slot is not yet filled, assign an empty tuple as its

    value.

    o If the keyword dictionary argument is not yet filled, assign an empty

    dicionary as its value.

    o For each remaining empty slot: if there is a default value for that

    slot, then fill the slot with the default value. If there is no default value, then it is an error.

当发生错误时,将打印有关错误的消息。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
试图读取字段“java”。lang.反对安卓。util。一对首先   gradle对java的影响。属性作为任务的输入?   java我的return语句不起作用。我做错了什么?   java通用同步代码块[无对象锁定]   macos Java小程序游戏不能在Mac上运行,即使它可以在PC上运行   java Firebase GeoFire设置位置未在服务中工作   java排序字符串对象   java通过JLabel上传背景图像(jpg)后,如何将图像缩放到它所在的面板上?   java无法连接到jsp中的数据库   java如何在整个ant taskdef操作的执行过程中拥有一个singleton类实例   java如何在按键事件中检查JTtextField中的字符串数字验证器   如何最好地输出大型单行XML文件(使用Java/Eclipse)?   内存不足,Java运行时环境无法在Java应用程序中继续运行   java Hibernate连接错误:组织上的NullPointerException。冬眠hql。阿斯特。HqlSqlWalker。createFromJoinElement   linux将参数从shell脚本传递到java类   java对于接受多个参数的setter是否有类似于@JsonCreator的注释?   java在链表中添加节点