commandline icon indicating copy to clipboard operation
commandline copied to clipboard

Support for implicit empty string

Open Der-Kraken opened this issue 4 years ago • 1 comments

I want to design an api with a bool-like option which can optionally contains a name. Below are my commands I wanted to work with:

// 1. Call MyFeature without adding a project
myfeature

// 2. Call MyFeature and add a project with default naming
myfeature --add-project

// 3. Call MyFeature and add a project with a specific name
myfeature --add-project "myprojectname"

Below is my options class:

[Verb("myfeature")]
public class MyOptions
{
    [Option("add-project", Required = false)]
    public string AddProjectName { get; set; }

    public bool AddProject => AddProjectName != null;
}

My problem is that the 2. solution is not working, because the engine forces the user to set a string arg. In order to set an empty string he has to be give an empty "". But that would not be an intuitive api.

Is there already a solution for this design problem? Can I disable the string-check for this item? I already set "Required" to false.

Der-Kraken avatar Jul 13 '21 06:07 Der-Kraken

Not sure if you fixed your issue yet, but you could add a default value to your option:

[Option("add-project",
        Default = "",
        Required = false)]
public string AddProjectName { get; set; }

More info on the Option Attribute page in the wiki.

mhbarnes avatar Nov 24 '21 18:11 mhbarnes