标签: docopt

docopt 布尔 arg python

我在 doctopt 脚本中使用以下参数

Usage:
GaussianMixture.py --snpList=File --callingRAC=File

Options:
-h --help     Show help.
snpList     list snp txt
callingRAC      results snp
Run Code Online (Sandbox Code Playgroud)

我想添加一个对我的脚本有条件结果的参数:更正我的数据或不更正我的数据。就像是 :

Usage:
GaussianMixture.py --snpList=File --callingRAC=File  correction(--0 | --1)

Options:
-h --help     Show help.
snpList     list snp txt
callingRAC      results snp
correction      0 : without correction | 1 : with correction 
Run Code Online (Sandbox Code Playgroud)

我想在我的脚本中添加if一些函数

def func1():
  if args[correction] == 0:
      datas = non_corrected_datas
  if args[correction] == 1:
      datas = corrected_datas
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在用法中编写它,也不知道如何在我的脚本中编写它。

python docopt

4
推荐指数
1
解决办法
2665
查看次数

使用 --docopt 解释命令和位置参数

这是来自 docopt.org 的示例:

Naval Fate.

Usage:
  naval_fate ship new <name>...
  naval_fate ship <name> move <x> <y> [--speed=<kn>]
  naval_fate ship shoot <x> <y>
  naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
  naval_fate -h | --help
  naval_fate --version

Options:
  -h --help     Show this screen.
  --version     Show version.
  --speed=<kn>  Speed in knots [default: 10].
  --moored      Moored (anchored) mine.
  --drifting    Drifting mine.
Run Code Online (Sandbox Code Playgroud)

我发现该Options:部分中的选项可以有很长的解释。举个例子,很明显naval_fate --version就是Show version。

但是,有没有办法为命令或位置参数提供扩展解释?例如,用户如何知道naval_fate ship shoot <x> <y>做什么?

command-line docopt

4
推荐指数
1
解决办法
494
查看次数

接受来自 docopt 的任意选项

浏览 docopt 文档和示例,我似乎找不到这个功能,但我觉得它应该存在,所以我想我会要求确认一下。

我正在使用 Python 的 docopt 并希望能够允许任意选项。该用例是一个命令行模板实用程序 - 因此任意键值都会很方便。

"""Templator

Usage:
    templator <template> [--arbitrary-option=<value>]...
"""
Run Code Online (Sandbox Code Playgroud)

希望这个例子能说明我所追求的。也许类似的东西--*=<value>是另一种书写方式。

编辑:

这是我当前的解决方案,它采用键值对。然而,它们是用空格分隔的,因此可能很难弄清楚长语句的键和值是什么。

templator <template> (<key> <value>)...

然后在 python 脚本中(对于对此解决方案感兴趣的任何人)

arguments = docopt(__doc_)
arbitrary_kwargs = dict(zip(arguments['<key>'], arguments['<value>']))
Run Code Online (Sandbox Code Playgroud)

key=value如果可能的话,拥有语法将是理想的选择。

python docopt

3
推荐指数
1
解决办法
495
查看次数

mutual_exclusive_group,带有可选和位置参数

我使用docopt创建了一个cli规范,但是由于某种原因我必须将它重写为argparse

Usage:
    update_store_products <store_name>...
    update_store_products --all

    Options:
      -a --all     Updates all stores configured in config
Run Code Online (Sandbox Code Playgroud)

怎么做?

什么是重要的我不想有这样的东西:

update_store_products [--all] <store_name>...
Run Code Online (Sandbox Code Playgroud)

我认为这将是这样的:

update_store_products (--all | <store_name>...)
Run Code Online (Sandbox Code Playgroud)

我尝试使用add_mutually_exclusive_group,但是我收到了错误:

ValueError: mutually exclusive arguments must be optional
Run Code Online (Sandbox Code Playgroud)

python argparse docopt

2
推荐指数
1
解决办法
588
查看次数

docopt失败,docopt.DocoptLanguageError:无法匹配'['

为什么此代码失败并出现以下异常?

"""my_program - for doing awesome stuff

Usage: my_program [--foo]

Options:
    --foo - this will do foo
"""

import docopt
args = docopt.docopt(doc=__doc__)
Run Code Online (Sandbox Code Playgroud)

例外:

Traceback (most recent call last):
  File "/tmp/post.py", line 10, in <module>
    args = docopt.docopt(doc=__doc__)
  File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 560, in docopt
    pattern = parse_pattern(formal_usage(DocoptExit.usage), options)
  File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 373, in parse_pattern
    result = parse_expr(tokens, options)
  File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 381, in parse_expr
    seq = parse_seq(tokens, options)
  File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", line 396, in parse_seq
    atom = parse_atom(tokens, options)
  File "/Users/rbednark/.virtualenvs/docopt-python2/lib/python3.5/site-packages/docopt.py", …
Run Code Online (Sandbox Code Playgroud)

python docopt

2
推荐指数
1
解决办法
531
查看次数

类型推导如何在这个Docopt示例中工作?

使用docopt库查看此代码:

const USAGE: &'static str = "...something...";

#[derive(Deserialize)]
struct Args {
    flag: bool,
}

type Result<T> = result::Result<T, Box<error::Error + Send + Sync>>;

fn main() {
    let mut args: Args = Docopt::new(USAGE)
        .and_then(|d| d.deserialize())
        .unwrap_or_else(|e| e.exit());
}
Run Code Online (Sandbox Code Playgroud)

如果你看一下等号右边的表达式,你会发现它没有在Args任何地方提到结构.编译器如何推导出该表达式的返回类型?类型信息可以在Rust中以相反的方向(从初始化目标到初始化表达式)流动吗?

type-inference rust type-deduction docopt

2
推荐指数
1
解决办法
73
查看次数

如何使用docopt传递多个参数

我想使用docopt传递两个强制参数,一个可选参数到我的程序.我使用的代码是:

"""Setup

Usage: myprog.py server_name config [--help] [options] 

Arguments:
    SERVER_NAME        Server Name (a1, a2)
    CONFIG             Config file with full path

Options:
    -h --help
    -r --start      Start the server if yes [default: 'no']
"""

from docopt import docopt

class ServerSetup(object):
    def __init__(self, server_name, config_file, start_server):
        self.server = server_name
        self.config = config_file
        self.start_server = start_server

    def print_msg(self):
        print self.server
        print self.config
        print self.start_server

if __name__ == '__main__':
    args = docopt(__doc__)
    setup = ServerSetup(server_name=args['SERVER_NAME']),
                        config=args['CONFIG']
                        start_rig=args['-r'])
    setup.print_msg()
Run Code Online (Sandbox Code Playgroud)

$ python myprog.py a1 /abc/file1.txt

当我使用上面的命令运行上面的程序时,我收到显示我写过的用法的错误消息.这里出了什么问题,我怎么能使用多个'Arguments'?

python docopt

1
推荐指数
1
解决办法
731
查看次数

docopt on python 3只打印帮助屏幕而不执行该功能

我是第一次在Python 3中使用docopt.当我运行代码时,输​​出只显示Usage选项数据,并且不执行模块中的功能.

这是一个例子.我在一个文件中有这个代码.

"""Usage:  scratch.py [-h] [--boston | --sandiego] [--prostitution |
        --drugs] [--lim VALUE]
    Options:
        -h --help        :Show help information
        --boston         : work on boston addresses
        --sandiego       : work on san diego addresses
        --prostitution  : prostitution incidents
        --drugs         : drug sale incidents
        --lim          : number of addresses to work with
"""


from docopt import docopt

def scratch(args):
    print(args['--boston'])
    print('hello')

if __name__ == '__main__':

    arg = docopt(__doc__, help=False)
    print(arg)
    scratch(arg)
Run Code Online (Sandbox Code Playgroud)

当我使用各种选项运行此文件时,我得到的只是文档字符串的副本.我应该看到docopt从docstring参数创建的字典的副本.然后我还应该看到函数调用的打印结果.但除了文档之外,我什么都看不到.

所以,如果我进行如下命令行调用:

python scratch.py --boston --drugs --lim 100
Run Code Online (Sandbox Code Playgroud)

返回的所有内容如下.

Usage:  scratch.py …
Run Code Online (Sandbox Code Playgroud)

python python-3.x docopt

1
推荐指数
1
解决办法
658
查看次数

docopt 必需选项和带参数的选项

我是 docopt 的新手,在让一个小例子工作时遇到一些困难。我现在遇到两个小问题,欢迎对这些问题提供帮助以及关于改进代码的更一般性评论。第一个问题是让程序需要该--required选项。它应该在运行时打印文档字符串,而无需所需的命令行选项。第二个问题是让程序接受选项(例如COMPUTER)的参数(例如--computer)。如何在终端中指定它以及如何编码?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
my example program

Usage:
    docopt_example_1.py [--ipaddress=IPADDRESS] [--computer=COMPUTER]
    docopt_example_1.py --network <network>
    docopt_example_1.py (--required)
    docopt_example_1.py --notrequired
    docopt_example_1.py --version

Arguments:
    IPADDRESS               I.P. address
    COMPUTER                computer identification
    <network>               network identification

Options:
    -h, --help              Show this help message.
    --version               Show the version and exit.
    --required              option required for running
    --notrequired           option not required for running
    --ipaddress=IPADDRESS   I.P. address
    --computer=COMPUTER     computer identification
    --network=NETWORK       network identification
"""
from docopt import …
Run Code Online (Sandbox Code Playgroud)

python arguments docopt

0
推荐指数
1
解决办法
6391
查看次数