jiacai2050 / zigcli

A toolkit for building command lines programs in Zig.
https://zigcli.liujiacai.net/
MIT License
48 stars 5 forks source link

Simargs support subcommands #22

Open jiacai2050 opened 3 months ago

jiacai2050 commented 3 months ago

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 commented 1 month ago

I prefer the union enum approach.

jiacai2050 commented 1 month ago

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