Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Comments

Add SubcommandChooser#20

Closed
yegle wants to merge 1 commit intomitchellh:masterfrom
yegle:master
Closed

Add SubcommandChooser#20
yegle wants to merge 1 commit intomitchellh:masterfrom
yegle:master

Conversation

@yegle
Copy link

@yegle yegle commented Jun 4, 2015

The SubcommandChooser can be used to customize the behavior when the
default map lookup cannot find the subcommand you want.

Example usage:

busybox-style subcommand invocation

// A symbolic link `ls` to binary `busybox` will invoke `busybox ls`
mySubcommandChooser = func(*c cli.CLI) (CommandFactory, error){
    subcommand = os.Args[0]
    if subcommand != c.Name() {
        if subcommandFunc, ok := c.Commands[subcommand]; ok {
            return subcommandFunc, nil
        }
    }
    return cli.DefaultSubcommandChooser(c)
}

c := cli.NewCLI("busybox", "1.0.0")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
    "ls": lsCommandFactory,
    "cat": catCommandFactory,
}
c.SubcommandChooser = mySubcommandChooser

default subcommand

// `myapp --help` == `myapp default --help`
mySubcommandChooser = func(*c cli.CLI) (CommandFactory, error) {
    if c.Subcommand() == "" {
        return c.Commands["default"], nil
    }
    return cli.DefaultSubcommandChooser(c)
}
c := cli.NewCLI("myapp", "1.0.0")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
    "default": defaultCommandFactory,
}
c.SubcommandChooser = mySubcommandChooser

@yegle yegle force-pushed the master branch 3 times, most recently from 91e3762 to 04b1925 Compare June 4, 2015 22:27
@yegle yegle force-pushed the master branch 4 times, most recently from b131bcf to bdce560 Compare June 4, 2015 23:05
The SubcommandChooser can be used to customize the behavior when the
default map lookup cannot find the subcommand you want.

Example usage:

1. busybox-style subcommand invocation

```
// A symbolic link `ls` to binary `busybox` will invoke `busybox ls`
mySubcommandChooser = func(*c cli.CLI) (CommandFactory, error){
    subcommand = os.Args[0]
    if subcommand != c.Name() {
        if subcommandFunc, ok := c.Commands[subcommand]; ok {
            return subcommandFunc, nil
        }
    }
    return cli.DefaultSubcommandChooser(c)
}

c := cli.NewCLI("busybox", "1.0.0")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
    "ls": lsCommandFactory,
    "cat": catCommandFactory,
}
c.SubcommandChooser = mySubcommandChooser
```

2. default subcommand

```
// `myapp --help` == `myapp default --help`
mySubcommandChooser = func(*c cli.CLI) (CommandFactory, error) {
    if c.Subcommand() == "" {
        return c.Commands["default"], nil
    }
    return cli.DefaultSubcommandChooser(c)
}
c := cli.NewCLI("myapp", "1.0.0")
c.Args = os.Args[1:]
c.Commands = map[string]cli.CommandFactory{
    "default": defaultCommandFactory,
}
c.SubcommandChooser = mySubcommandChooser
```
@yegle
Copy link
Author

yegle commented Jun 5, 2015

Did several update, now contains improved unit test coverage. PTAL.

@yegle
Copy link
Author

yegle commented Jul 30, 2015

ping?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant