ikskuh/zig-args

Implement support for subcommands

ikskuh opened this issue · 2 comments

Using a tagged union instead of a struct for implementing sub commands:

const options = try argsParser.parse(union(enum) {
    fibonacci: struct {
        limit: usize,
    },
    primes: struct {
        limit: usize,
        algorithm: enum { naive, sieve } = .naive,
    },
}, &args, argsAllocator);

This would allow cmd fibonacci --limit and cmd primes --algorithm sieve

What about something like:

const GitArgs = struct {
  options: struct {
      C: zig_args.FilePath,
  },
  sub_commands: union {
    show: struct {
      options: struct {
        bar: bool = false,
      },
      arguments: struct {
        commit: []u8,
      },
    },
  };
};

The subcommands are just nested versions of that top-level struct. Each command would have fields options, subcommands and possibly other fields to set properties for that level.

Resolved in edaeb0f