Skip to content

Cannot parse empty string as argument of option #1551

Closed

Description

@KalleOlaviNiemitalo

With System.CommandLine 2.0.0-beta2.21617.1, if I define a command like this

using System;
using System.CommandLine;

internal static class Program
{
    private static int Main(string[] args)
    {
        var execPrefixOption = new Option<string>("--exec-prefix", getDefaultValue: () => "/usr/local");
        var rootCommand = new RootCommand()
        {
            execPrefixOption,
        };
        rootCommand.SetHandler(
            (string execPrefix) => Console.WriteLine(execPrefix + "/bin"),
            execPrefixOption);
        return rootCommand.Invoke(args);
    }
}

and run the program with arguments --exec-prefix "", then I expect it to override the default value "/usr/local" with an empty string, but it instead reports an error:

Required argument missing for option: '--exec-prefix'.

--exec-prefix="" has the same result.

The #1533 fix in 260a559 looks related, but I don't know whether this has worked in earlier versions, either.

Activity

  1. elgonzo commented on Dec 27, 2021

    @elgonzo
    Contributor

    but I don't know whether this has worked in earlier versions, either.

    Both --exec-prefix "" as well as --exec-prefix="" still worked in 2.0.0-beta1.21524.1.

    2.0.0-beta1.21525.1 changed the behavior of --exec-prefix="" but left --exec-prefix "" still intact.

    It is also broken in some way, currently (2.0.0-beta2.21623.1). If one wants to define an Argument<string> additionally to the Option<string> from the code in the report above, System.CommandLine gets confused when a commandline like

    --exec-prefix "" abc
    

    or

    --exec-prefix="" abc
    

    is provided. In these situations, System.CommandLine will mistake the value abc as the --exec-prefix option argument.
    Dotnetfiddle: https://dotnetfiddle.net/ORb4sz


    In my subjective and cheap opinion, changing the behavior of --exec-prefix="" and then later also of --exec-prefix "" was the wrong call.

    I see no good reason to prohibit a user from providing an empty commandline argument like --exec-prefix "".

    Since --exec-prefix="" as well as --exec-prefix:"" are supposed to be synonymous with --exec-prefix "", for consistency reasons both --exec-prefix="" and --exec-prefix:"" should be allowed as well.

    Which in the end means, due to how commandline arguments are processed and fed into the args parameter of the Main method (at least on Windows), that --exec-prefix= and --exec-prefix: should be allowed too, representing the option with an implied empty value. I would find this the lesser evil than just outright prohibiting empty option arguments. (Just my two cents, of course...)

  2. added this to the 2.0 GA milestone on Feb 3, 2022
  3. self-assigned this
    on Feb 3, 2022
  4. added a commit that references this issue on Feb 4, 2022
    d237ba6
  5. ilyapalkin commented on Jul 7, 2023

    @ilyapalkin

    I face the similar error on 2.0.0-beta4.22272.1 when provide --exec-prefix=""

    My code:

            var execPrefix = new Option<string>("--exec-prefix",  description: "Exec prefix");
            ...
            var rootCommand = new RootCommand()
            {
                execPrefix ,
                ...
            };
            var parsed = rootCommand.Parse(args);
            if (!parsed.Errors.Any())
            {  ...  }
            else {
                ...
                // print errors returns "Required argument missing for option: '--exec-prefix'."
            }
    

    How to handle empty srtring as a parameter valu?

  6. added a commit that references this issue on Mar 6, 2024
    55d6172
  7. ViktorHofer commented on Oct 30, 2026

    @ViktorHofer
    Member

    The --exec-prefix="" half of this report is still reproducible on 3.0.0-rc.3.26522.148. 55d6172 fixed the separate-argv form (--exec-prefix "") by removing the arg.Length > 0 guard, but TrySplitIntoSubtokens still discards an empty remainder, so --exec-prefix= yields Required argument missing. Filed as #2872 with a repro, since this issue is closed.

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

Metadata

Metadata

Assignees

Labels

Area-Parser and Binderbug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions