Skip to content

Assigning default values #836

Closed
Closed

Description

@markolbert

I initially constructed my options using a pattern like this:

var population = new Option<int>(
    new[] { "-p", "--population" },
    getDefaultValue: () => 1000,
    description : "Number of people to simulate" );

But I found default values weren't showing up after parsing the command line, so I switched the pattern to this:

var population = new Option<int>(
    new[] { "-p", "--population" },
    description : "Number of people to simulate" )
    {
        Argument = new Argument<int>(()=>1000)
    };

This works find. But I don't understand what's wrong with the original pattern. What am I missing?

BTW, this is a >>fantastic<< package! Good luck in continuing to hone it!

Activity

  1. jonsequitur commented on Apr 1, 2020

    @jonsequitur
    Contributor

    Your first sample works for me when using 2.0.0-beta1.20158.1. Can you check the version you're using?

    image

  2. markolbert commented on Apr 1, 2020

    @markolbert
    Author

    That's the same release I'm using (just wrote the code yesterday). Offhand the one difference I see between your test code and what I originally did was that I set Argument within the Option initializer, even before I included the default value Func within the Argument constructor. Could that have made a difference? Is is it necessary to set Argument or does it get created by default by the Option constructor?

  3. jonsequitur commented on Apr 1, 2020

    @jonsequitur
    Contributor

    Ah! The Option<T> constructor delegates to the Argument property, so you might have overwritten the getDefaultValue from the constructor.

    The Option.Argument property is a bit confusing in this way and we've considered removing or hiding it.

    @KathleenDollard Any thoughts on this?

  4. markolbert commented on Apr 1, 2020

    @markolbert
    Author

    I confirmed that moving the default value setter into the Option constructor and eliminating setting Argument in the initializer resolves the issue I cited.

    Apropos of whether or not to hide or remove the Argument property, is there another way of setting a validator on an Option? I'm using a pattern like this:

    var population = new Option<int>(
        new[] { "-p", "--population" },
        getDefaultValue : () => 1000,
        description : "Number of people to simulate" );
    
        population.Argument.AddValidator(
            x => x.GetValueOrDefault<int>() < 100 ? "Population must be >= 100" : null );
    
  5. jonsequitur commented on Apr 3, 2020

    @jonsequitur
    Contributor

    There isn't currently but we would need to address that in order to hide the Option.Argument property.

    Another proposed change for validators is to have them operate on bound values rather than tokens, so you could just write something like this:

    population.Argument.AddValidator(
            x => x < 100 ? "Population must be >= 100" : null );
  6. markolbert commented on Apr 3, 2020

    @markolbert
    Author

    That would be a nice enhancement, too. Again, a wonderful library!

    Shall I close this thread?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions