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

examples/macros.rs failed when running without arguments #10

Open
Daimonion1980 opened this issue Jun 3, 2024 · 6 comments
Open

examples/macros.rs failed when running without arguments #10

Daimonion1980 opened this issue Jun 3, 2024 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@Daimonion1980
Copy link

Daimonion1980 commented Jun 3, 2024

I have another behavior that possibly could be fixed.

When running the example examples/macros.rs and not providing any arguments (i.e. not providing the -n argument) the application panicks instead of printing the help message.

I think the application crashes when try to execute the macro in line 426
The get function which is called by this macro works, but after that something weired is going on I do not understand. ;)
The first argument -h is processed correctly from __parse_arg and the .get function. This returns ArgumentRef.default_value as ok. After this the next call from __pars_arg crashes.

Failure message at this line is: "Tried to unwrap argument that wasn't passed"

@Daimonion1980 Daimonion1980 added the bug Something isn't working label Jun 3, 2024
@Daimonion1980 Daimonion1980 changed the title examples/mqcro.rs failed when running without arguments examples/macros.rs failed when running without arguments Jun 3, 2024
@Kyllingene
Copy link
Owner

This is the intended behavior of sarge: notice how there's no #ok/#err before the argument, which indicates that if you don't pass it, it'll panic.

Perhaps I could change the panic message, or change the example to put print a message rather than panicking?

@Kyllingene
Copy link
Owner

Hmm, when cleaning up the macros example, I noticed its help generation is also not right. I'm investigating now, but I don't know what could be causing it.

@Daimonion1980
Copy link
Author

notice how there's no #ok/#err before the argument, which indicates that if you don't pass it, it'll panic.

Actually I use #ok, but maybe #err is what I need. I will look into this.

Perhaps I could change the panic message, or change the example to put print a message rather than panicking?

I am writing a CLI Application where 3 string option arguments are mandatory and when they are not given the application shall print a failure message together with the help output.
The example/macros.rs code fit this needs, because when parsing fails an error message and the help is printed.
But when I try to use this example the only way I can handle the 3 mandatory args is when using it with #ok/#err and check if the parsed option/option<Result<T,_>> are provided with values != default(T)

Furthermore I am not a fan of code which panics. ;)

Hmm, when cleaning up the macros example, I noticed its help generation is also not right. I'm investigating now, but I don't know what could be causing it.

Maybe there is a failure which led to this panic.
At the moment I don't understand this line

Thank you for your investigation!

@Kyllingene
Copy link
Owner

But when I try to use this example the only way I can handle the 3 mandatory args is when using it with #ok/#err and check if the parsed option/option<Result<T,_>> are provided with values != default(T)

Unfortunately, there's not much I can do about error messages: besides some panics, error handling is outside the purview of sarge, so #ok is what you need for ensuring strings are passed. There's not much I can do about that, since everyone handles errors differently. Therefore, the current panicking behavior is intended, though I've updated the example to instead handle the error.

Maybe there is a failure which led to this panic.

The issue isn't a panic; it's that subsequent lines of documentation don't get printed with indentation. I really can't figure out why, the expanded code seems like it ought to work just fine.

At the moment I don't understand this line

__parse_arg is what's responsible for handling how each argument gets handled. Depending on which tag (#ok/#err/none) gets passed, and whether or not there's a default given, the macro will handle missing arguments and parsing errors differently.

Notably, if you give an argument that has no default value (for example, bools always default to false if not passed, while strings have no default) no tag, and it doesn't get passed, sarge has no option but to panic. What else could it do? You told it not you give you an error, so the only thing it can do is panic on the spot.

@Daimonion1980
Copy link
Author

Hmm, I understand.

Think that my not understanding is because I'm pretty new to rust and don't know how the code and macros works.
Until I understand the code I am fine with #err and default values on my string arguments.

Thank you!

@Kyllingene
Copy link
Owner

Happy to help! Note that for string arguments, you probably want #ok: all err gets you is letting you know if the argument fails to parse, but strings can't fail to parse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants