Comments (11)
I don't think what you describe exists today. However, I'm having a hard time imagining a good use case for such a feature. Could you maybe give a short example of how such a feature would be used?
from commander.
@ggilder Sure. The use I have in mind is for global options. As far as I'm aware, there is no way to invoke the #default
method of the option struct before you're inside an action block. This requires you to repeat code if you want to set defaults on global options. The same goes for any processing of the global options that'd you like to do (e.g. loading some data from a database depending on an option value)-- if a value is passed, you can do this in block of the global_option
method, but not if you want to do processing on a default value.
from commander.
Hmm, maybe I'm mistaken but setting defaults seems pretty straightforward already. I'd probably use something like the following:
GLOBAL_DEFAULTS = { foo: 'bar' }
global_option('--foo VALUE') { |val| GLOBAL_DEFAULTS[:foo] = val }
Similarly with command options, the default
method just takes a hash so it's easy enough to extract that:
COMMAND_DEFAULTS = { prefix: 'foo' }
command :bar do |c|
c.option '--prefix STRING', String, 'Adds a prefix to bar'
c.action do |args, options|
options.default COMMAND_DEFAULTS
end
end
# repeat with other commands...
As far as post-processing the options, the examples you give seem pretty domain-specific, so my instinct would be to delegate that to your own classes outside of the commander DSL:
class AppSetup
def self.setup(options)
# your own logic to do db queries, set up objects, etc.
end
end
command :bar do |c|
c.action do |args, options|
# delegate post-processing, setup, etc. to your domain objects
AppSetup.setup(options)
end
end
Does that address the use cases you're thinking of? If not, maybe a short code sample of what you're imagining would be helpful for discussion.
from commander.
What @smackesey is seems to be describing and what I see useful is to allow initialization based on global options. For example a program doing DB query may want to open a DB connection regardless of command in use. And without need for all commands to call same initialization method.
def run
program :name, 'MongoQuery'
default_command :ui
command :ui do |c|
...
end
pre_command do |c|
c.action do |args, global_options|
# put common initialization here
end
end
end
...
from commander.
@akostadinov practically speaking, couldn't you just run that initialization code before the commander stuff? e.g.:
def run
# put common initialization here
program :name, 'MongoQuery'
default_command :ui
# etc...
end
I don't really see what advantage you'd get by putting that in a commander hook.
from commander.
@ggilder , in your example I don't have access to parsed global_options
. That's the issue. e.g. I don't have access to database credentials before parameters are parsed.
from commander.
Thanks, I think I see the use case now. I think this would be a relatively straightforward change to Commander::Runner#parse_global_options
. If you're interested in putting together a pull request I can help with any questions you have.
from commander.
Please comment on pull #96
from commander.
On the other hand this whole feature might be stupid after all. If I want to get a database, then I can have a method to get me the DB connection object (in contrast with setting up an @db
variable within the after_global_options hook). When I call that #db
method it will either return @db
if it exists or try creating a new connection.
I'm not sure there are any valid use cases.
Another thing is that if one does setup in the after_global_options hook, then why do it if command is help
for example?
Everybody interested, please comment.
from commander.
I guess my assumption is that on something like help
the user is not providing the necessary global options that would cause the expensive connection to get set up.
However, I personally agree with the skepticism — for something that needs a complex level of setup I would probably prefer to have that managed by my own module, using Commander as the translation between CLI and this module.
from commander.
This issue was moved to commander-rb/commander#5
from commander.
Related Issues (20)
- Upgrade to rubocop 0.29.1; bump gem HOT 2
- Unable to build a automatic test tool that tests the commander Js's command action with a return value. HOT 1
- Mandatory options? HOT 9
- Highlight code in README.rdoc plz HOT 1
- Question : ctrl-c and listener HOT 1
- The `find` statement fails on Windows HOT 2
- c.option '-f', '--foo-bar' ... dash in option name issue HOT 1
- add support for option terminator -- HOT 1
- Can't figure out how to use commander using the "Modular" style HOT 5
- when specifying options, the "--no" version of a flag must appear after the flag HOT 2
- Use HighLine menu system HOT 4
- -t and --trace confused HOT 1
- Support for pry HOT 9
- Ruby warning: circular require considered harmful HOT 1
- --help should display the command flags/options of the default command HOT 2
- Using `always_trace!` should hide the `--trace` option in the help output HOT 2
- Option parsing error (incomplete option name) HOT 4
- Able to access options set by global_option() outside of the command block HOT 9
- Default help formatter hard limits command length to 20 characters HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from commander.