davetron5000 / gli

Make awesome command-line applications the easy way
http://davetron5000.github.io/gli
Apache License 2.0
1.26k stars 102 forks source link

Add the ability to pass in flags to compound commands #249

Closed JakeLaCombe closed 3 years ago

JakeLaCombe commented 8 years ago

Let's say I have a command like the following

command :release_projects => [
    :release_documentation,
    :release_tasks
] 

I would like to ability to run the command as followed

release_projects --default_version=3.9.1

However, I don't see a good way of being able to do this. Is GLI capable of passing flags to compound commands? If so, could we get a pull request to document how that would be done?

davetron5000 commented 8 years ago

What happens when you try to do this? i.e. if you give release_tasks a flag of default-version, does that work?

JakeLaCombe commented 8 years ago

when I run release_project --default_version=3.9.1, I get the following error

error: Unknown option --default_version

I did set the flag in the other commands as followed

command :release_documentation do |c_release_doc|
  c_release_doc.desc 'dry run: print the release comment that would be posted in JIRA'
  c_release_doc.flag [:dry]
  c_release_doc.action do |globals, options, args|

    if options[:dry]
      puts 'Flag is set'
    end
  end
end

command :release_tasks do |c_release_tasks|
  c_release_tasks.desc 'dry run: print the release comment that would be posted in JIRA'
  c_release_tasks.flag [:dry]
  c_release_tasks.action do |globals, options, args|

    if options[:dry]
      puts 'Flag is set'
    end
  end
end
davetron5000 commented 8 years ago

OK, that's what I thought. How would you envisioning this worked? Would the compound command get all the flags and switches of its two commands? If so, how do we resolve duplicates?

(I'm guessing those questions are why I haven't implemented this yet :)

JakeLaCombe commented 8 years ago
Would the compound command get all the flags and switches of its two commands?

I would have to say yes for this instance. If a developer is running a bundled command, I believe it can be assumed that they would like consistent behavior across the various commands, meaning all commands would have the same starting state and values.

If so, how do we resolve duplicates?

Are you referring to the case I have above, where dry is found in both of the commands? I feel for a case like that, then we would want to maintain the same values for those flags.

davetron5000 commented 8 years ago

What about a case like this:

command :release_documentation do |c_release_doc|
  c_release_doc.desc 'dry run: print the release comment that would be posted in JIRA'
  c_release_doc.flag [:dry, :'dry-run'] # <----
  c_release_doc.action do |globals, options, args|

    if options[:dry]
      puts 'Flag is set'
    end
  end
end

command :release_tasks do |c_release_tasks|
  c_release_tasks.desc 'Do not repeat any tasks'
  c_release_tasks.flag [:dry, :d] # <--
  c_release_tasks.action do |globals, options, args|

    if options[:dry]
      puts 'Flag is set'
    end
  end
end

Would the assumption be that the developer knows not to do this?

JakeLaCombe commented 8 years ago

I will agree that when you get to this route, that assumptions will get more difficult. I'lll see if I can get other developers' opinions on this topic as well.

davetron5000 commented 3 years ago

Closing as old. Please re-open if you still need this.