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

Unparsing skips arguments with values equal to the argument type's default value #850

Open
El-Gor-do opened this issue Sep 24, 2022 · 3 comments

Comments

@El-Gor-do
Copy link

Using CommandLineParser 2.9.1, the unparsing behaviour with SkipDefault = true seems to be incorrect. Here is a test app to reproduce the behaviour.

using CommandLine;

namespace UnparsingTest
{
    public class Program
    {
        public class TestArgs
        {
            [Option('c', Required = true)]
            public int CompulsoryWithoutDefault { get; set; }

            [Option('d', Required = true, Default = 1)]
            public int CompulsoryWithDefault { get; set; } = 1;

            [Option('o', Required = false, Default = 1)]
            public int Optional { get; set; } = 1;
        }

        static void Main()
        {
            Parser parser = new Parser();

            for (int i = 0; i <= 2; ++i)
            {
                TestArgs args = new TestArgs()
                {
                    CompulsoryWithoutDefault = i,
                    CompulsoryWithDefault = i,
                    Optional = i,
                };

                string commandLine = parser.FormatCommandLine(args, settings => settings.SkipDefault = true);
                System.Console.WriteLine($"CompulsoryWithDefault: {args.CompulsoryWithDefault}, CompulsoryWithoutDefault: {args.CompulsoryWithoutDefault}, Optional: {args.Optional}, Command line: \"{commandLine}\"");
            }
        }
    }
}

There are 2 compulsory arguments, one with a Default value and one without a Default value. CompulsoryWithDefault and Optional have their Default values set to any value that is not default(int).

The test app's console output shows the unparsed command line for the 3 sets of arguments

CompulsoryWithDefault: 0, CompulsoryWithoutDefault: 0, Optional: 0, Command line: ""
CompulsoryWithDefault: 1, CompulsoryWithoutDefault: 1, Optional: 1, Command line: "-c 1"
CompulsoryWithDefault: 2, CompulsoryWithoutDefault: 2, Optional: 2, Command line: "-c 2 -d 2 -o 2"

The unparsed command line for the first case will fail round trip parsing because the compulsory -c and -d arguments are missing and -o is missing so Optional will have a value of 1 instead of 0. The unparsed command line for the second case will also fail round trip parsing because the compulsory -d argument is missing. Only the third case is giving correct output.

The bug appears to be that the unparser is skipping Required = true arguments, arguments with value equal to Default = {value} and arguments with value equal to default(typeof(argument)).

The correct behaviour would be to only skip arguments that do not have Required = true and whose value equals the value specified in Default = {value}.

The expected console output would be

CompulsoryWithDefault: 0, CompulsoryWithoutDefault: 0, Optional: 0, Command line: "-c 0 -d 0 -o 0"
CompulsoryWithDefault: 1, CompulsoryWithoutDefault: 1, Optional: 1, Command line: "-c 1 -d 1"
CompulsoryWithDefault: 2, CompulsoryWithoutDefault: 2, Optional: 2, Command line: "-c 2 -d 2 -o 2"

These command lines would then correctly round trip parse/unparse.

@El-Gor-do
Copy link
Author

The code causing this behaviour is in

private static bool IsEmpty(this object value, Specification specification, bool skipDefault)

Changing the function body to the code below makes unparsing behave as I described in the original post.

        private static bool IsEmpty(this object value, Specification specification, bool skipDefault)
        {
            if (value == null) return true;

            // always output Required arguments
            if (specification.Required) return false;

            if (skipDefault && value.Equals(specification.DefaultValue.FromJust())) return true;
            if (Nullable.GetUnderlyingType(specification.ConversionType) != null) return false; //nullable

#if !SKIP_FSHARP
            if (ReflectionHelper.IsFSharpOptionType(value.GetType()) && !FSharpOptionHelper.IsSome(value)) return true;
#endif
            // ValueType arguments with Default != default(T) and value == default(T) should be output
            //if (value is ValueType && value.Equals(value.GetType().GetDefaultValue())) return true;
            if (value is string && ((string)value).Length == 0) return true;
            if (value is IEnumerable && !((IEnumerable)value).GetEnumerator().MoveNext()) return true;
            return false;
        }

This line forces Required arguments to always be output:

            if (specification.Required) return false;

Commenting out this line allows value arguments to be output when Default != default(T) and value == default(T):

            //if (value is ValueType && value.Equals(value.GetType().GetDefaultValue())) return true;

@katjoek
Copy link

katjoek commented Sep 28, 2022

I was also very surprised to see arguments being skipped for options that are required with SkipDefault set to true. The generated command line string cannot be successfully parsed always as required options may not be present.

I require a workaround now. The value of enum type I'm using will start explicitly from 1 so that the default value of 0 will not be used and the enum will be unparsed always.

@El-Gor-do
Copy link
Author

I raised a PR #852 which fixes this bug.

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

No branches or pull requests

2 participants