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

cli: extend control over defaults #21308

Merged
merged 3 commits into from
Apr 20, 2024
Merged

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Apr 18, 2024

The PR extends functionality of the cli module to allow more control over defaults.

Creating an application with the cli module and default commands and will result in something like:

Usage: <cool_upcoming_app> [flags] [commands] <paths>

Flags:
  -i  --include       Lorem ipsum dolor.
  -h  --help          Prints help information.
  -v  --version       Prints version information.
      --man           Prints the auto-generated manpage.

Commands:
  help                Prints help information.
  version             Prints version information.
  man                 Prints the auto-generated manpage.

An example result with more direct control over defaults could look like (keeps some default flags):

Usage: <cool_upcoming_app> [flags] <paths>

Flags:
  -i  --include       Lorem ipsum dolor.
  -h  --help          Prints help information.
  -v  --version       Prints version information.

Current customization possibilities are limited as they only disable flag and command at once.

mut app := cli.Command{
	// ...
	disable_man: true
	disable_help: true
	disable_version: true
}

The updated functionality would allow full control

mut app := cli.Command{
	// ...
	defaults: struct{
		man: false
		help: cli.CommandFlag{false,true} // Fine grained control is also possible.
		version: cli.CommandFlag{false,true}
	}

}

The current disable_man, disable_help, disable_version flags are deprecated but will continue to work.

I had this potential change sitting since October but didn't submit it because there were some issues regarding its implementation. After the recent development and since private fields are now actually treated as private to a module, this change can work out. Also, I find myself in the situation again where I'd like to be able to easily disable default flags or commands but not both when working with the cli module. So I'd like to give it a shot.

Would just need to be refined and covered with a test.

Comment on lines +51 to +54
pub struct CommandFlag {
pub mut:
command bool = true
flag bool = true
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If DefaultOptions or something would be better fitting - the name is open for suggestions.

// TODO: remove deprecated `disable_<>` switches after deprecation period.
fn (mut cmd Command) parse_defaults() {
// Help
if cmd.defaults.help is bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A exhaustive type match like this match cmd.defaults.help { bool {...} CommandFlag {} }, is usually preferable, since if we later change the sumtype to type Defaults = CommandFlag | bool | string, the code will break in exactly the places, that need modification.

@spytheman spytheman merged commit e235641 into vlang:master Apr 20, 2024
44 checks passed
@ttytm ttytm deleted the cli/extend-defaults branch April 20, 2024 09:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants