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

WIP: Command groups #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

VoxSecundus
Copy link

This PR introduces the ability for apps using Commander to define command groups/subcommands.

Overview

The syntax for groups is similar to how one would define a command. A group is given a name, a syntax string, and optionally a summary/description. Groups are displayed at the top-level help dialog alongside ungrouped commands (grouped commands are no longer shown in the top-level help dialog). Each group has its own help dialog describing the subcommands within that group, which is shown when the user enters the group name without specifying a subcommand.

In-practise

The following block will define:

  • A group named files; with:
    • A summary
    • A description
    • Two subcommands
  • An ungrouped command named set-default
group :files do |g|
  g.summary = 'Perform file operations'
  g.syntax = 'files SUBCOMMAND'

  g.command :list do |c|
    c.description = 'List files in the specified directory'
    c.syntax = "files list"
    c.action ListCommand
  end

  g.command :delete do |c|
    c.description = 'Delete specified file'
    c.syntax = "files delete"
    c.action DeleteCommand
    c.slop.bool '-r', '--recursive', 'Delete a directory and all of its contents'
  end
end

command 'set-default' do |c|
  c.syntax = 'set-default'
  c.description = 'Set or view the default directory'
  c.action SetDefaultCommand
end

The default help dialog for the above would resemble the following:

  NAME:

    program_name

  DESCRIPTION:

    program_description

  COMMANDS:
	
    files       Perform file operations		
    help        Display global or [command] help documentation		
    set-default Set or view the default directory	

  GLOBAL OPTIONS:
	
    -h, --help
        Display help documentation
	
    --version
        Display version information

With the help dialog for files resembling the following:

 SYNOPSIS:

    files SUBCOMMAND

  DESCRIPTION:

    Perform file operations

  SUBCOMMANDS:

    delete Delete the specified file
    list   List files in the specified directory

Future enhancements

Currently, we only allow single-level groups; as in, a group can only contain subcommands. In the future, it may be useful to implement group nesting.

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.

1 participant