Uh oh!
There was an error while loading. Please reload this page.
subcommand parser - #41
Conversation
I was just looking to suggest something like this, after using the Crystal What do you think about the ability to make Here's the example from the Crystal website: verbose=falsesalute=falsewelcome=falsename="World"parser=OptionParser.newdo |parser|
parser.banner="Usage: example [subcommand] [arguments]"parser.on("salute","Salute a name")dosalute=trueparser.banner="Usage: example salute [arguments]"parser.on("-t NAME","--to=NAME","Specify the name to salute"){ |_name| name=_name}endparser.on("welcome","Print a greeting message")do# This UX is very symmetric with the restwelcome=trueparser.banner="Usage: example welcome"# Re-uses original parser in scope - which might affect visitor pattern of Ruby's optparse?endparser.on("-v","--verbose","Enabled verbose output"){verbose=true}parser.on("-h","--help","Show this help")doputsparserexitendendparser.parseifsaluteSTDERR.puts"Saluting #{name}"ifverboseputs"Hello #{name}"elsifwelcomeSTDERR.puts"Welcoming #{name}"ifverboseputs"Welcome!"elseputsparserexit(1)end |
kwatch
commented
Oct 3, 2024
I think that it is NOT a good idea to support subcommand functionality in OptionParser. I strongly recommend for OptionParser to focus on its role as command option parsing. |
kwatch
commented
Oct 5, 2024
I released cliapp gem which is a small framework for CLI application similar to Git or Docker. Example code of cliapp (filename: #!/usr/bin/env rubyrequire'cliapp'## create an application objectapp=CLIApp.new("Sample","Sample Application",version: "1.0.0")app.global_options({:help=>["-h","--help","print help message"],:version=>["--version","print version number"],:list=>["-l","--list","list action names"],})## 'hello' actionapp.action("hello","greeting message",{:lang=>["-l","--lang=<en|fr|it>","language",["en","it","fr"]],})do |name="world",lang: "en"|
caselangwhen"en";puts"Hello, #{name}!"when"fr";puts"Bonjour, #{name}!"when"it";puts"Chao, #{name}!"elseraise"** internal error: lang=#{lang.inspect}"endend## mainbeginapp.run(*ARGV)exit0rescueOptionParser::ParseError,CLIApp::ActionError=>exc
$stderr.puts"[ERROR] #{exc.message}"exit1endExample output: $ chmod ./sample
$ ./sample hello # subcommand exampleHello, world!
$ ./sample hello --lang=fr # subcommand option exampleBonjour, world!
$ ./sample hello Alice --lang=fr # subcommand argument exampleBonjour, Alice!Again, I strongly recommend for OptionParser to focus on its role as command option parsing. |
No description provided.