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

Simargs support subcommands #22

Closed
jiacai2050 opened this issue Apr 1, 2024 · 2 comments · Fixed by #23
Closed

Simargs support subcommands #22

jiacai2050 opened this issue Apr 1, 2024 · 2 comments · Fixed by #23

Comments

@jiacai2050
Copy link
Owner

jiacai2050 commented Apr 1, 2024

For now, simargs doesn't sub commands, add subcommands support will make it much more powerful.

The syntax I could think of is like this:

const App = struct {
    help: bool = false,
    output: []const u8,

    sub_command1: ?struct {
        flags1: int,
    },

    sub_command2: ?struct {
        flags1: int,
    },
};

Since there are more than one subcommand, we use optional to annotate them.

Or we can represent subcommand with union(enum)

const App = struct {
    help: bool = false,
    output: []const u8,

    sub_command: union(enum) {
        sub_cmd1: struct {
            flag1: i8,
        },
        sub_cmd2: struct {
            flag1: i8,
        },
    },
};

Here both method use nested struct to represent subcommands, and it can be nested at any-level!

The help message would look like this:

Usage: mycli [Global-Options] [COMMAND] [Options]

Commands:
  sub_command1  xxx
  sub_command2  yyy

Options:
  -o, --output <file>           Output file
  -h, --help                    Print help
@clickingbuttons
Copy link

I prefer the union enum approach.

@jiacai2050
Copy link
Owner Author

Thanks, I also like the union style. I will implement this feature in next few weeks.

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 a pull request may close this issue.

2 participants