Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow boolean flags without automatically creating a --no-option version #553

Closed
whopper opened this issue Mar 9, 2017 · 4 comments
Closed

Comments

@whopper
Copy link

whopper commented Mar 9, 2017

Context: I'm working on a command that needs a boolean flag, we'll call it --list. If this flag is provided, I want to display a list of items. Otherwise, the list will not be displayed. Currently, if I add this option like so:

method_option :list, type: :boolean

Thor automatically creates a --no-list option, which I don't need and is confusing in the help output:

Options:
  [--list], [--no-list]

This is due to this line: https://github.com/erikhuda/thor/blob/db16e19ad4bc797c580a2646ab54606fd0fd294f/lib/thor/parser/option.rb#L91

Thor should be able to create a boolean flag like this with a default value (false in this case) and not create the negation of the option.

Alternatively, perhaps this is just bad style? Should I be implementing list as a subcommand? Any thoughts are appreciated!

@piotrmurach
Copy link

Hey Will, I think I would agree with you on this one. It feels that thor is assuming too much for its own good. If you consider Ruby standard optparse it specifically requires you to provide [no-]list for it to generate both versions of the flag. I can imagine that thor could provide api to add this without forcing it as a defeault like so:

method_option "[no-]list", type: :boolean
# or
method_option :list, type: boolean, negate: true

@aks
Copy link

aks commented Feb 7, 2018

A related issue is how to provide an alias for the negated boolean option that is separate from the positive boolean option?

For example, if the my CLI is uses this option:

   option :unicode, aliases: '-U', type: :boolean, default: true, desc: "Use Unicode in the displays"

What is the best way to provide an alias for the negation?

As a work-around, I've created opposite booleans, that are mutually exclusive. Eg:

 option :unicode, aliases: '-U', type: :boolean, default: true,  desc: "Use Unicode in the displays"
 option :ascii,   aliases: '-A', type: :boolean, default: false, desc: "Use ASCII in the displays"

@schneems
Copy link
Member

schneems commented Feb 9, 2023

I hit this today. I have a case where I want to delete all of something. It doesn't make sense to delete not-all of something:

$ bin/hatchet help destroy
Usage:
  hatchet destroy

Options:
  [--all], [--no-all]  # Delete ALL hatchet apps
  [--older-than=N]     # Delete all hatchet apps older than N minutes

Deletes application(s)

@rafaelfranca
Copy link
Member

Duplicated of #417

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants