Hejsil / zig-clap

Command line argument parsing library
MIT License
939 stars 67 forks source link

Sub commands #1

Closed Hejsil closed 5 years ago

Hejsil commented 6 years ago

Allow the clap to parse subcommands.

const parser = comptime Clap(Options).Builder
    .init(
        Options {
            .print_values = false,
            .a = 0,
            .b = 0,
            .c = 0,
        }
    )
    .command(
        Command.Builder
            .init("com")
            .arguments(
                []Argument {
                    Argument.Builder
                        .init("a")
                        .help("Set the a field of Option.")
                        .short('a')
                        .takesValue(true)
                        .build(),
                }
            )
            .subCommands(
                []Command {
                    Command.Builder
                        .init("sub-com")
                        .arguments(
                            []Argument {
                                Argument.Builder
                                    .init("b")
                                    .help("Set the a field of Option.")
                                    .short('b')
                                    .takesValue(true)
                                    .build(),
                            }
                        )
                        .build()
                }
            )
            .build()
    )
    .build();

Subcommands have their own options, which cannot be accessed by their parent. com -a 1 sub-com -b 1 works, but com -a 1 -b 1 does not.

Questions:

Hejsil commented 5 years ago

This can be implemented as a wrapper on top of StreamingClap.

Hejsil commented 5 years ago

Out of scope. This is a small package for - and -- style argument parsing only. One could implement a package on top of zig-clap that does this.