标签: optparse

解析Python中的空选项

我有一个应用程序,允许您将事件数据发送到自定义脚本.您只需布置命令行参数并分配哪些事件数据与哪个参数相关.问题是这里没有真正的灵活性.您将映射的每个选项都将被使用,但并非每个选项都必须包含数据.因此,当应用程序构建要发送到脚本的字符串时,某些参数为空,并且python的OptionParser错误输出"error: - someomegument option需要参数"

因为有超过200个数据点,所以我不能编写单独的脚本来处理可能的参数的每个组合(它需要2 ^ 200个脚本).有没有办法在python的optionparser中处理空参数?

python optparse optionparser

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

Python optparse不适合我

我目前正在学习如何使用Python optparse模块.我正在尝试以下示例脚本,但args变量为空.我尝试使用Python 2.5和2.6,但无济于事.

import optparse

def main():
  p = optparse.OptionParser()
  p.add_option('--person', '-p', action='store', dest='person', default='Me')
  options, args = p.parse_args()

  print '\n[Debug]: Print options:', options
  print '\n[Debug]: Print args:', args
  print

  if len(args) != 1:
    p.print_help()
  else:
    print 'Hello %s' % options.person

if __name__ == '__main__':
  main() 
Run Code Online (Sandbox Code Playgroud)

输出:

>C:\Scripts\example>hello.py -p Kelvin

[Debug]: Print options: {'person': 'Kelvin'}

[Debug]: Print args: []

Usage: hello.py [options]
Run Code Online (Sandbox Code Playgroud)

选项:-h, - help显示此帮助消息并退出-p PERSON, - person = PERSON

python optparse

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

从命令行选项创建数组(python :: optparse)

有一个python脚本从命令行读取基准名称,如下所示:

-b benchname1
Run Code Online (Sandbox Code Playgroud)

这个代码的代码是:

import optparse
import Mybench
parser = optparse.OptionParser()
# Benchmark options
parser.add_option("-b", "--benchmark", default="", help="The benchmark to be loaded.")
if options.benchmark == 'benchname1':
  process = Mybench.b1
elif options.benchmark == 'benchname2':
  process = Mybench.b2
else:
  print "no such benchmark!"
Run Code Online (Sandbox Code Playgroud)

我想要做的是为这个命令行创建一个基准数组:

-b benchname1 benchname2
Run Code Online (Sandbox Code Playgroud)

所以"进程"应该是一个数组:

process[0] = Mybench.b1
process[1] = Mybench.b2
Run Code Online (Sandbox Code Playgroud)

对此有什么建议吗?

感谢名单

python arrays optparse command-line-arguments

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

Python,optparse和文件掩码

if __name__=='__main__':
    parser = OptionParser()
    parser.add_option("-i", "--input_file", 
                    dest="input_filename",
                      help="Read input from FILE", metavar="FILE")

    (options, args) = parser.parse_args()
    print options
Run Code Online (Sandbox Code Playgroud)

结果是

$ python convert.py -i video_*
{'input_filename': 'video_1.wmv'}
Run Code Online (Sandbox Code Playgroud)

当前文件夹中有video_ [1-6] .wmv.问题是video_*成为video_1.wmv的原因.我做错了什么?

python optparse

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

在python脚本中使用命令行参数

我对python比较陌生.我想写一个脚本并传递它的参数如下:

myscript.py --arg1=hello --arg2=world
Run Code Online (Sandbox Code Playgroud)

在脚本中,我想访问参数arg1和arg2.任何人都可以解释如何以这种方式访问​​命令行参数?

python optparse argparse

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

在R中通过命令行传递多个参数

我试图通过命令行将多个文件路径参数传递给Rscript,然后可以使用参数解析器进行处理.最终我会想要这样的东西

Rscript test.R --inputfiles fileA.txt fileB.txt fileC.txt --printvar yes --size 10 --anotheroption helloworld -- etc...
Run Code Online (Sandbox Code Playgroud)

通过命令行传递,并在解析时将结果作为R中的数组

args$inputfiles =  "fileA.txt", "fileB.txt", "fileC.txt"
Run Code Online (Sandbox Code Playgroud)

我尝试了几种解析器,包括optparse和getopt,但它们似乎都不支持这种功能.我知道argparse确实如此,但它目前不适用于R版本2.15.2

有任何想法吗?

谢谢

command-line arguments r optparse argparse

3
推荐指数
2
解决办法
6464
查看次数

获取"AttributeError:'tuple'对象没有属性"并且无法理解原因

我在这个项目中使用python 2.4,所以我使用的是optparse.运行此代码时出现以下错误:

Traceback (most recent call last):
  File "./clientNFSLatMonME.py", line 49, in ?
    debug,verbose,interval = parseOptions()
  File "./clientNFSLatMonME.py", line 43, in parseOptions
    if (args.interval < 1) or (args.interval > MAX_INTERVAL):
AttributeError: 'tuple' object has no attribute 'interval'
Run Code Online (Sandbox Code Playgroud)

代码如下:

MAX_INTERVAL = 1800

def parseOptions():

    parser = OptionParser()
    parser.add_option("-d", "--debug",    dest="debug",    action="store_true", help="enable additional debugging output")
    parser.add_option("-v", "--verbose",  dest="verbose",  action="store_true", help="enable additional console output")
    parser.add_option("-i", "--interval", dest="interval", action="store", type="int", default=900, help="specify the time interval, default is 900, maximum is 1800")

    args = …
Run Code Online (Sandbox Code Playgroud)

python optparse python-2.4

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

使用`optparse`模拟Python脚本的命令行参数?

我想要使​​用(调用snakefood)的Python脚本通常从命令行运行并获取命令行参数,例如:

sfood /path/to/my/project
Run Code Online (Sandbox Code Playgroud)

的命令行参数解析发生在一个名为gendeps.py使用optparse.但是,我想从另一个脚本中使用snakefood模块.有没有办法可以以某种方式模拟命令行参数的传递snakefood或重写方式,gendeps.py以便它不再依赖于它optparse

python optparse command-line-arguments

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

将用户 arg 传递给 DESeq2 函数

DESeq我正在尝试使用从命令行输入的参数在 RScript 中运行。我曾经optparse解析用户参数并尝试将设计参数传递到函数中DESeqDataSetFromMatrix()

我直接测试了这个功能,效果很好:

DESeq_tbl <- DESeqDataSetFromMatrix(countData=counts_tbl,
colData=coldata, design=~taxonomy)
Run Code Online (Sandbox Code Playgroud)

但是,如果我尝试传递变量opt$design(这是一个字符串=“~taxonomy”),我会收到以下错误:

DESeq_tbl <- DESeqDataSetFromMatrix(countData=counts_tbl,
colData=coldata, design=opt$design)
Run Code Online (Sandbox Code Playgroud)

错误:$ 运算符对于原子向量无效执行停止

我尝试过/noquote()的各种组合并将整个命令创建为字符串以传递给函数,但没有任何效果。任何建议将不胜感激。catpasteDESeqDataSetFromMatrix()

解决方案

感谢 Ben Bolker 下面的回答,以下方法有效:

DESeq_tbl <- DESeqDataSetFromMatrix(countData=counts_tbl,
colData=coldata, design=as.formula(opt$design))
Run Code Online (Sandbox Code Playgroud)

arguments r optparse string-parsing bioconductor

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

R命令行参数的optparse错误

出于某种原因,optparse此脚本中的用法会中断:

test.R:

#!/usr/bin/env Rscript
library("optparse")

option_list <- list(
    make_option(c("-n", "--name"), type="character", default=FALSE,
                dest="report_name", help="A different name to use for the file"),
    make_option(c("-h", "--height"), type="numeric", default=12,
                dest = "plot_height", help="Height for plot [default %default]",
                metavar="plot_height"),
    make_option(c("-w", "--width"), type="numeric", default=10,
                dest = "plot_width", help="Width for plot [default %default]",
                metavar="plot_width")
)

opt <- parse_args(OptionParser(option_list=option_list), positional_arguments = TRUE)
print(opt)

report_name <- opt$options$report_name
plot_height <- opt$options$plot_height
plot_width <- opt$options$plot_width

input_dir <- opt$args[1] # input directory
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

    $ ./test.R --name "report1" --height 42 …
Run Code Online (Sandbox Code Playgroud)

r optparse

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