Skip to content

Names and aliases cannot contain whitespace #50368

@walcht

Description

@walcht

Type of issue

Code doesn't work

Description

In the Parsing and invocation in System.CommandLine documenation page, the following code snippet has a space in its aliases that produces a System.ArgumentException: Names and aliases cannot contain whitespace: "A URL" (Parameter 'alias'):

using System.CommandLine;

class Program
{
    // <asyncaction>
    static Task<int> Main(string[] args)
    {
        // the "A URL" alias should not contain a whitespace   something like "url"
        Option<string> urlOption = new("--url", "A URL.");
        // ...
    }

    // ...
}

Suggested new snippet:

using System.CommandLine;

namespace commandlinetest;

public static class Program
{
  static Task<int> Main(string[] args)
  {
    Option<string> urlOption = new("--url", "url");
    RootCommand rootCommand = new("Handle termination example") { urlOption };

    rootCommand.SetAction((parseResult, cancellationToken) =>
    {
      string? urlOptionValue = parseResult.GetValue(urlOption);
      return DoRootCommand(urlOptionValue, cancellationToken);
    });

    return rootCommand.Parse(args).InvokeAsync();
  }

  public static async Task<int> DoRootCommand(
      string? urlOptionValue, CancellationToken cancellationToken)
  {
    using HttpClient httpClient = new();

    try
    {
      HttpResponseMessage response = await httpClient.GetAsync(urlOptionValue, cancellationToken);
      Console.WriteLine(response);
      return 0;
    }
    catch (OperationCanceledException)
    {
      await Console.Error.WriteLineAsync("The operation was aborted");
      return 1;
    }
  }
}

Source file: /main/docs/standard/commandline/snippets/handle-termination/csharp/Program.cs

Page URL

https://learn.microsoft.com/en-us/dotnet/standard/commandline/how-to-parse-and-invoke

Content source URL

https://github.com/dotnet/docs/blob/main/docs/standard/commandline/how-to-parse-and-invoke.md

Document Version Independent Id

f0b218fa-d4d0-7ff8-7198-a21616f6b903

Platform Id

f03627a0-d939-4a82-5259-6f0ae325956a

Article author

@gewarren

Metadata

  • ID: e039f4b3-f5c1-b965-0dac-b2eb75b12210
  • PlatformId: f03627a0-d939-4a82-5259-6f0ae325956a
  • Service: dotnet-fundamentals

Related Issues


Associated WorkItem - 537844

Metadata

Metadata

Assignees

Type

No type

Projects

Status

✅ Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions