commander-rb / commander

The complete solution for Ruby command-line executables
MIT License
822 stars 74 forks source link

Cannot define my own -v and --version params #64

Closed dcepelik closed 5 years ago

dcepelik commented 6 years ago

The -v and --version default global options prevent me from defining my own -v and --version parameters to commands. (As the tool I'm writing deals with versions, this is a bummer.)

Is it really impossible to prevent Commander from adding these defaults, or have I missed something? In the former case, I'll be happy to submit a patch.

Commander is really great, thanks!

ggilder commented 6 years ago

Unfortunately that's not possible right now. If you'd like to submit a patch, the global options are added here: https://github.com/commander-rb/commander/blob/master/lib/commander/runner.rb#L60 And I would suggest disabling that with a switch similar to #always_trace! (https://github.com/commander-rb/commander/blob/master/lib/commander/runner.rb#L99) or maybe a key for #program (https://github.com/commander-rb/commander/blob/master/lib/commander/runner.rb#L141)

dcepelik commented 6 years ago

Hello @ggilder, thanks for the reply. Unfortunately RSpec behaves really strange with commander on my machine. Each time a different number of tests is run, and some tests are failing with a fresh repo. I suspect this is a bug in either Ruby or RSpec (perhaps related to randomization). Have you ever seen anything like that? Tested with Ruby 2.3.1, 2.4.3 and 2.5.0 (using rvm).

ggilder commented 6 years ago

How are you running RSpec? bundle exec rspec or bundle exec rake run consistently for me (and on Travis CI).

dcepelik commented 6 years ago

I'm using bundle exec rake spec and bundle exec rake, both suffer the problem I've described. bundle exec rspec indeed seems to give consistent results with ~10 consecutive runs, except that the number of reported examples (87) is quite different to what my colleague's machine reports (121).

dcepelik commented 6 years ago

@ggilder, sorry to bother you, but is there any further help you could provide?

ggilder commented 6 years ago

The correct number of examples is 121 as you can see from the Travis CI logs: https://travis-ci.org/commander-rb/commander

I don't think I've ever seen a problem like what you're describing. I'm not really sure why your environment would be behaving like that, but my first suggestion would probably be to uninstall and reinstall the version of ruby you're using.

You mentioned that "some tests are failing with a fresh repo" — do you have examples of the failures? Maybe that will reveal something.

dcepelik commented 6 years ago

I've already tried reinstalling my Ruby. Unfortunately that wouldn't fix the issue.

One of the failing tests (the other are passing now for some reason), using bundle exec rake spec:

1) Commander with invalid option passed to --help should output an invalid option message
   Failure/Error:
     expect do
       run('--help', 'test', '--invalid-option')
     end.to raise_error(SystemExit, /invalid option: --invalid-option/)

     expected SystemExit with message matching /invalid option: --invalid-option/, got #<SystemExit: exit> with backtrace:
       # ./lib/commander/runner.rb:391:in `parse_global_options'
       # ./lib/commander/runner.rb:68:in `run!'
       # ./spec/spec_helper.rb:62:in `run'
       # ./spec/runner_spec.rb:475:in `block (4 levels) in <top (required)>'
       # ./spec/runner_spec.rb:474:in `block (3 levels) in <top (required)>'
   # ./spec/runner_spec.rb:474:in `block (3 levels) in <top (required)>'
Finished in 0.05696 seconds (files took 0.13285 seconds to load)
63 examples, 1 failure

Failed examples:

rspec ./spec/runner_spec.rb:473 # Commander with invalid option passed to --help should output an invalid option message

Randomized with seed 39452

Coverage report generated for RSpec to /home/david/sw/commander/coverage. 350 / 467 LOC (74.95%) covered.

Please let me know if I can provide further information.

ggilder commented 6 years ago

Is your checkout of commander up to date? Those line numbers don't look right based on the master branch.

The only other things that are coming to mind are, maybe you have some weird RSpec options in ~/.rspec? Kind of grasping at straws here.

dcepelik commented 6 years ago

@ggilder Found the error, it was my mistake all the time. I didn't actually test against a vanilla repo. I haven't realised I've done a modification I considered too trivial to break anything, but as it turns out, it actually caused this hassle. (I would still like to find out how it's possible for RSpec to succeed with 78 examples when there are 121 and not complain about anything. I'm new to Ruby and still surprised at many things!)

Anyway, thanks for help, greatly appreciated. I'll provide the patch soon.

ggilder commented 5 years ago

Closing this issue since it hasn't been updated in a long time — please feel free to reopen or submit a PR if this is still relevant. Thanks!