prajwalch/yazap

`CommandArgumentNotProvided` error when `argRequired` is set to false

musaprg opened this issue · 1 comments

Hi, I want to show a help message when no arguments are passed or the "-h" flag is passed.
I set the argRequired option to false, but it returned an error when no arguments passed.

env

  • yazap v0.2.0
  • Zig v0.9.1

Current behavior

...
var parser = Command.new(allocator, "sample");
defer parser.deinit();

var subcmd = Command.new(allocator, "init");
try subcmd.addArg(flag.boolean("help", 'h'));
try subcmd.takesSingleValue("FOOBAR");
subcmd.argRequired(false);
try parser.addSubcommand(subcmd);

var args = try parser.parseProcess();
defer args.deinit();
...
error(zigarg): The command 'sample' requires a value but none is provided

error: CommandArgumentNotProvided
/src/lib/zig-arg/src/parser/Parser.zig:415:13: 0x25401b in .zig-arg.parser.Parser.parseSubCommand (sample)
            return self.err_ctx.err;
            ^
/src/lib/zig-arg/src/parser/Parser.zig:147:28: 0x24ee37 in .zig-arg.parser.Parser.parse (sample
            const subcmd = try self.parseSubCommand(token.value);
                           ^
/src/lib/zig-arg/src/Command.zig:168:9: 0x248e9b in .zig-arg.Command.parseFrom (sample)
        return e;
        ^
/src/lib/zig-arg/src/Command.zig:160:5: 0x243703 in .zig-arg.Command.parseProcess (sample)
    return self.parseFrom(self.process_args.?[1..]);
    ^
/src/src/main.zig:345:16: 0x23cb37 in main (sample)
    var args = try parser.parseProcess();

Expected behavior

no error when "argRequired" is false.

I guess the issue comes from around here. It should take care of this situation and suppress the error when "argRequired" is false.

https://github.com/PrajwalCH/yazap/blob/4153a51dea5f1c3771a85fbff1b9b330988a73db/src/parser/Parser.zig#L413-L415

Error message also seems incorrect.

Let me fix this two and push the patch.