是否可以使用 Python 中的 optparse 模块制作自己的帮助消息或在帮助选项上附加自己的事件?
我在解析命令行选项时遇到以下代码问题
#!/usr/bin/env ruby
require 'optparse'
options = {:username=>nil}
optparse = OptionParser.new do|opts|
# Define the options, and what they d
opts.on( "--username", "Cassandra username" ) do |username|
options[:username] = username
end
opts.on( '--password', 'Cassandra password' ) do |password|
options[:password] = password
end
opts.on( '--keyspace', 'Cassandra keyspace' ) do |keyspace|
options[:keyspace] = keyspace
end
end
puts ARGV
optparse.parse!(ARGV)
puts options
Run Code Online (Sandbox Code Playgroud)
当我从命令行运行此代码时
./testopt.rb --username=uuuuu --password=xxxxxx --keyspace=test
Run Code Online (Sandbox Code Playgroud)
我得到以下输出
--username=uuuuu
--password=xxxxxx
--keyspace=test
./testopt.rb:25:in `<main>': needless argument: --username=uuuuu (OptionParser::NeedlessArgument)
Run Code Online (Sandbox Code Playgroud)
缺少什么?我在ruby 2.2.3p173 (2015-08-18 revision 51636) …
当脚本使用是这样的时候,Python optparse非常有用
%prog [options] [args]
Run Code Online (Sandbox Code Playgroud)
但我需要为脚本编写1个必需参数的帮助,所以用法就是这样的
%prog action [options] [args]
Run Code Online (Sandbox Code Playgroud)
使用Subversion时可以看到类似的东西 - 它的用法字符串是
svn <subcommand> [options] [args]
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:是否有可能以Subversion的方式为optparse准备所需的参数帮助?结果我希望看到这样的帮助:
Usage: python myscript.py action [options] [args]
Available actions:
foo
bar
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-v, --verbose Verbose mode. Output debug log to stdout.
Run Code Online (Sandbox Code Playgroud) 我经常发现自己这样做:
optparse = OptionParser.new do |opts|
options[:directory] = "/tmp/"
opts.on('-d','--dir DIR', String, 'Directory to put the output in.') do |x|
raise "No such directory" unless File.directory?(x)
options[:directory] = x
end
end
Run Code Online (Sandbox Code Playgroud)
如果我可以指定Dir或Pathname代替,那会更好String.是否有模式或我的Ruby式方式这样做?
有没有办法我可以在python中配置optparse而不采取开头-?而不是
%program -d optionvalue
Run Code Online (Sandbox Code Playgroud)
我明白了
%program d optionvalue
Run Code Online (Sandbox Code Playgroud)
目前,当我尝试做的时候
parser.add_option('d', '--database')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
optparse.OptionError: invalid option string 'd': must be at least two characters long
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激!谢谢
关于是否使用2.x或3.x,我已经看过其他几个主题.但是,其中大部分至少有两年的历史,不区分2.6和2.7.
我正在重新启动一个我最终可能要在2013年之前发布的科学项目.我在标准的2.6+模块(如itertools)中使用了numpy,scipy和pylab.哪个版本,2.6或2.7,会更好?
这也将清除在制作脚本时是否使用optparse.
编辑:我在大学工作,我拿起的工作站有Python 2.4.2.6到2.7之间的选择决定了要升级到哪个发行版.感谢您的建议!
我正在尝试做一些看起来像这样的事情:
opt_parser = OptionParser.new do |opt|
opt.banner = "Test"
opt.separator ""
opt.on("-t", "--test arg1 arg2", "Test") do |arg1, arg2|
puts arg1
puts arg2
end
end
Run Code Online (Sandbox Code Playgroud)
问题是它返回arg1,但arg2返回nil.如何使这项工作?
我正在使用 argparse,我想要类似的东西: test.py --file hello.csv
def parser():
parser.add_argument("--file", type=FileType('r'))
options = parser.parse_args()
return options
def csvParser(filename):
with open(filename, 'rb') as f:
csv.reader(f)
....
....
return par_file
csvParser(options.filename)
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:TypeError 强制转换为 Unicode:需要字符串或缓冲区,找到文件。
我怎么能解决这个问题?
如何使用optparse而不是命令行参数解析自定义字符串?
我想解析一个我从中使用的字符串raw_input().我怎样才能使用optparse呢?
来自流行的optparse库的介绍性代码片段:
data Sample = Sample
{ hello :: String
, quiet :: Bool }
sample :: Parser Sample
sample = Sample
<$> strOption -- Q1
( long "hello"
<> metavar "TARGET" -- Q2
<> help "Target for the greeting" )
<*> switch
( long "quiet"
<> help "Whether to be quiet" )
Run Code Online (Sandbox Code Playgroud)
有关我的问题/疑惑,请参阅代码段中的注释.
Q1:如何将它<$>用作类型构造函数的第一个参数Sample?我认为这个操作必须在函数和仿函数之间使用.
Q2:<>此代码段中使用的操作是什么?