GithubHelp home page GithubHelp logo

post-argument-parsing hook about commander HOT 11 CLOSED

smackesey avatar smackesey commented on September 18, 2024
post-argument-parsing hook

from commander.

Comments (11)

ggilder avatar ggilder commented on September 18, 2024

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.

smackesey avatar smackesey commented on September 18, 2024

@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.

ggilder avatar ggilder commented on September 18, 2024

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.

akostadinov avatar akostadinov commented on September 18, 2024

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.

ggilder avatar ggilder commented on September 18, 2024

@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.

akostadinov avatar akostadinov commented on September 18, 2024

@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.

ggilder avatar ggilder commented on September 18, 2024

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.

akostadinov avatar akostadinov commented on September 18, 2024

Please comment on pull #96

from commander.

akostadinov avatar akostadinov commented on September 18, 2024

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.

ggilder avatar ggilder commented on September 18, 2024

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.

ggilder avatar ggilder commented on September 18, 2024

This issue was moved to commander-rb/commander#5

from commander.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.