Ruby 中非常便宜的命令行选项解析 [英] Really Cheap Command-Line Option Parsing in Ruby

查看:36
本文介绍了Ruby 中非常便宜的命令行选项解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回复之前阅读本文底部列出的两个要求.人们不断发布他们的新 gem 和库等等,显然不符合要求.

Please, please, please read the two requirements listed at the bottom of this post before replying. People keep posting their new gems and libraries and whatnot, which clearly don't meet the requirements.

有时我想非常便宜地将一些命令行选项破解成一个简单的脚本.一个有趣的方法是:

Sometimes I want to very cheaply hack some command line options into a simple script. A fun way to do it, without dealing with getopts or parsing or anything like that, is:

...
$quiet       = ARGV.delete('-d')
$interactive = ARGV.delete('-i')
...
# Deal with ARGV as usual here, maybe using ARGF or whatever.

这不是普通的 Unix 选项语法,因为它会接受选项非选项命令行参数,如myprog -i foo bar -q",但我可以接受.(有些人,比如 Subversion 开发人员,更喜欢这个.有时我也喜欢.)

It's not quite the normal Unix options syntax, because it will accept options non-option command line parameters, as in "myprog -i foo bar -q", but I can live with that. (Some people, such as the Subversion developers, prefer this. Sometimes I do too.)

不能比上述更简单地实现一个存在或不存在的选项.(一个赋值,一个函数调用,一个副作用.)有没有一种同样简单的方法来处理带有参数的选项,比如-f filename"?

An option that's just present or absent can't be implemented much more simply than the above. (One assignment, one function call, one side effect.) Is there an equally simple way to deal with options that take a parameter, such as "-f filename"?

我之前没有提出的一点,因为直到 Trollop 的作者提到该库适合一个 [800 行] 文件"时,我才明白我正在寻找仅适用于干净的语法,但适用于具有以下特征的技术:

One point I didn't make earlier on, because it hadn't become clear to me until the author of Trollop mentioned that the library fit "in one [800-line] file," is that I'm looking not only for clean syntax, but for a technique that has the following characteristics:

  1. 整个代码都可以包含在脚本文件中(而不会使实际脚本本身不堪重负,它可能只有几十行),这样就可以将单个文件放入 bin 目录在任何具有标准 Ruby 1.8.[5-7] 安装的系统上并使用它.如果您无法编写一个没有 require 语句的 Ruby 脚本,并且解析几个选项的代码不到十几行,那么您就无法满足此要求.

  1. The entirety of the code can be included in the script file (without overwhelming the actual script itself, which may be only a couple of dozen lines), so that one can drop a single file in a bin dir on any system with a standard Ruby 1.8.[5-7] installation and use it. If you can't write a Ruby script that has no require statements and where the code to parse a couple of options is under a dozen lines or so, you fail this requirement.

代码小而简单,人​​们可以记住足够多的内容,可以直接输入可以实现目标的代码,而不是从其他地方剪切和粘贴.想一想您在没有 Internet 访问权限的防火墙服务器的控制台上的情况,并且您想将一个快速脚本放在一起供客户端使用.我不了解你,但是(除了没有满足上述要求)即使是 45 行简化的 micro-optparse 也不是我愿意做的事情.

The code is small and simple enough that one can remember enough of it to directly type in code that will do the trick, rather than cutting and pasting from somewhere else. Think of the situation where you're on the console of a firewalled sever with no Internet access, and you want to toss together a quick script for a client to use. I don't know about you, but (besides failing the requirement above) memorizing even the 45 lines of simplified micro-optparse is not something I care to do.

推荐答案

这是我通常使用的标准技术:

Here's the standard technique I usually use:

#!/usr/bin/env ruby

def usage(s)
    $stderr.puts(s)
    $stderr.puts("Usage: #{File.basename($0)}: [-l <logfile] [-q] file ...")
    exit(2)
end

$quiet   = false
$logfile = nil

loop { case ARGV[0]
    when '-q' then  ARGV.shift; $quiet = true
    when '-l' then  ARGV.shift; $logfile = ARGV.shift
    when /^-/ then  usage("Unknown option: #{ARGV[0].inspect}")
    else break
end; }

# Program carries on here.
puts("quiet: #{$quiet} logfile: #{$logfile.inspect} args: #{ARGV.inspect}")

这篇关于Ruby 中非常便宜的命令行选项解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆