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

Allow zero-length list of argument values to be terminated by next option #155

Open
Pentamix opened this issue Nov 11, 2022 · 0 comments
Open

Comments

@Pentamix
Copy link

Pentamix commented Nov 11, 2022

PowerArgs allows options of type List<> to have 0, 1, or more argument values. However, if a List<> option is followed by another option on the command line, and the List<> option contains zero argument values, then PowerArgs parses the next option's key as an argument value of the preceding List<> option, which is undesired behavior.

For example, consider the following two options:

   [ArgDescription("Specify 0, 1, or more comma- or space-separated strings.")]
   public List<string> AlphaList { get; set; }
         
   [ArgDescription("Specify a flag.")]
   public bool BetaFlag { get; set; }

When specifying 1 or more argument values for -AlphaList, PowerArgs recognizes that the argument value list terminates at the next option, -BetaFlag:

   command.exe -AlphaList first -BetaFlag                   ==> AlphaList = {"first"}
   command.exe -AlphaList first second -BetaFlag            ==> AlphaList = {"first", "second"}
   command.exe -AlphaList first second third -BetaFlag      ==> AlphaList = {"first", "second", "third"}

Those all work as desired: -BetaFlag appears to be recognized as a terminator for the preceding option's parameter list.

However, when there are 0 argument values for -AlphaList, PowerArgs fails to recognize -BetaFlag as a terminator:

   command.exe -AlphaList -BetaFlag                         ==> AlphaList = {"-BetaFlag"}

In this case, the string "-BetaFlag" is interpreted as an argument value of -AlphaList. This is undesirable behavior: it is inconsistent with the behavior above, and typically causes the application to fail out.

The behavior we'd like to see is:

   command.exe -AlphaList -BetaFlag                         ==> AlphaList = {}

This should assign AlphaList to a List instance that contains no items (i.e. is empty).

There are two workarounds, but we don't find these friendly:

  1. In Powershell, at least, specify an empty argument value for the List<> option using double-quotes, with a space character between them:
   command.exe -AlphaList " " -BetaFlag                    ==> AlphaList = {}
  1. Put the list option at the end of the command line:
   command.exe -BetaFlag -AlphaList                        ==> AlphaList = {}

It's unfriendly to end users to expect them to know these, or to try to explain it to them in the command help. The second workaround also prevents multiple empty list options from being specified: only one option can be at the end of the command line, so other list options would require the first workaround as well.

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

1 participant