Skip to content

Setting ArgumentArity on string Option disables Option's default value #1010

Open

Description

@mahardan-oup

Consider the following code

using System;
using System.CommandLine;
using System.CommandLine.Invocation;

namespace zzCmdLineTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var command = new RootCommand
            {
                new Option<string>("--a-string", getDefaultValue: () => "goodbye"),
                new Option<int>("--an-int")
            };

            command.Handler = CommandHandler.Create(
                (string aString, int anInt) =>
                {
                    Console.WriteLine(aString);
                    Console.WriteLine(anInt);
                });

            command.InvokeAsync(args);
        }
    }
}

Building, running and parsing gives the expected results with the default string value

C:\Code\zzCmdLineTest>dotnet run [parse]
[ zzCmdLineTest *[ --a-string <goodbye> ] ]

C:\Code\zzCmdLineTest>dotnet run
goodbye
0 

Now change the code to add an Arity on the string parameter like this

var command = new RootCommand
{
    new Option<string>("--a-string", getDefaultValue: () => "goodbye")
    { Argument = new Argument<string>() { Arity = ArgumentArity.ZeroOrOne }},
    new Option<int>("--an-int")
};

Building, running and parsing shows that the default value for the string variable no longer applies

C:\Code\zzCmdLineTest>dotnet run [parse]
[ zzCmdLineTest ]

C:\Code\zzCmdLineTest>dotnet run

0

I would expect the default value to remain in tact whether or not I had explicitly set that the Option was optional.

Activity

  1. jonsequitur commented on Aug 6, 2020

    @jonsequitur
    Contributor

    This is a design flaw that can possibly be avoided by removing the Option.Argument property so that when you need to customize it, you're not replacing the instance. We would then have something like:

    new Option<string>("--a-string", getDefaultValue: () => "goodbye")
    { 
        Arity = ArgumentArity.ZeroOrOne 
    }

    Thoughts?

  2. mahardan-oup commented on Aug 6, 2020

    @mahardan-oup
    Author

    Having the Arity property on the Option class makes more sense to me. It's actually how I assumed it worked before I looked into the docs properly, so I would be all for this change.

  3. added
    bug
    on Aug 6, 2020
  4. heaths commented on Mar 28, 2021

    @heaths

    Ran into this as well, but as a workaround you can do this:

    var command = new RootCommand
    {
        new Option<string>("--a-string")
        {
            Argument = new Argument<string>(() => "goodbye")
            {
              Arity = ArgumentArity.ZeroOrOne,
            },
        },
        new Option<int>("--an-int")
    };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-APIbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions