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

Add required an default value support #107

Closed
wants to merge 2 commits into from

Conversation

voroskoi
Copy link

@voroskoi voroskoi commented Nov 2, 2023

This is not ready yet, I just push it for reference.

Tasks:

  • add required/optional support to the parser
  • add default value support to the parser
  • add more tests for various required/optional cases
  • give error, when a required parameter is missing (eg. -m is mandatory)
  • also check if the provided parameter type is correct (it should be done already)
  • make sure that optional value is a ?value and the required is just value
  • add bool for supporting on/off switches
  • handle the case when bool is 0/off/false as off and everything else as on
  • set the default value when there is no parameter provided
  • fix the commit messages before asking for a merge

Closes #82

@voroskoi voroskoi marked this pull request as draft November 2, 2023 06:14
Copy link
Owner

@Hejsil Hejsil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll just leave some comments as you go.

@@ -92,6 +92,7 @@ pub fn Param(comptime Id: type) type {
id: Id,
names: Names = Names{},
takes_value: Values = .none,
required: bool = false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think required only makes sense for things that takes values and probably only for taking 1 value. So I would modify the Values enum to something along the lines of:

pub const Values = enum {
    none,
    zero_or_one,
    one,
    many,
};

I'd argue that there is no place for optional and required many. Optional many is 0..N values and required would be 1..N. But why only support 1..N and not 2..N, 3..N and so on.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ofc, the question then becomes. How is many parsed? Is it --many <v>... or --many [v]...? I would argue for <v>... as each value is not optional, but there can be 0..N of them

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me [v]... means that I can omit the flag. <v>... indicates that I have to add the flag with at least one value. This could be even enhanced later to at least k values, but that does not seem a severe requirement to me.
If I want only the flag without value there is <bool>.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me [v]... means that I can omit the flag. ... indicates that I have to add the flag with at least one value.

Hmm. Alright. I think we have to look out and see what the standard is here and pick that :) I'll try to have a look around later.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I quite like this: http://docopt.org/
The project is sort-of dead, but the idea is nice imo. After element... You can find a couple of examples.

Comment on lines +33 to +50
/// Parse boolean value. False, off and 0 considered false, everything else is true.
pub fn boolean(in: []const u8) error{ParameterTooLong}!bool {
// check if input is not too long
const MAX_LEN = 16;
if (in.len >= MAX_LEN) return error.ParameterTooLong;

// convert to lowercase
var lower: [MAX_LEN]u8 = undefined;
for (in, 0..) |c, i| {
lower[i] = std.ascii.toLower(c);
}

if (std.mem.eql(u8, "0", lower[0..in.len]) or
std.mem.eql(u8, "off", lower[0..in.len]) or
std.mem.eql(u8, "false", lower[0..in.len])) return false;
return true;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Idk about this one. This seems quite opinionated and I can easily imagine anyone would expect different behaviour for bool values.

  • Not wanting the default to true behaviour. If someone passes of that does not seem like a true to me.
  • yes/no could be wanted
  • Someone might not wanna have 0/1 as that seems quite "programmer" specific

People should use flags for boolean options in most cases. If they don't want a default, they can easily just use parsers.enumeration(enum { on, off }) or whatever else they like.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I will make those changes.

@voroskoi
Copy link
Author

voroskoi commented Nov 3, 2023

This idea got dropped, see discussion in original issue.

@voroskoi voroskoi closed this Nov 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for required parameter/argument
2 participants