Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
define Directive symbols
  • Loading branch information
adamsitnik committed Feb 21, 2023
commit 0d76a52a4af320294d791319899534740fb79f7e
46 changes: 46 additions & 0 deletions src/System.CommandLine/Directive.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System.Collections.Generic;
using System.CommandLine.Completions;

namespace System.CommandLine
{
/// <summary>
/// The purpose of directives is to provide cross-cutting functionality that can apply across command-line apps.
/// Because directives are syntactically distinct from the app's own syntax, they can provide functionality that applies across apps.
///
/// A directive must conform to the following syntax rules:
/// * It's a token on the command line that comes after the app's name but before any subcommands or options.
/// * It's enclosed in square brackets.
/// * It doesn't contain spaces.
/// </summary>
public class Directive : Symbol
{
/// <summary>
/// Initializes a new instance of the Directive class.
/// </summary>
/// <param name="name">The name of the directive. It can't contain whitespaces.</param>
/// <param name="description">The description of the directive, shown in help.</param>
public Directive(string name, string? description)
{
if (string.IsNullOrWhiteSpace(name))
{
throw new ArgumentException("Name cannot be null, empty, or consist entirely of whitespace.");
}

for (var i = 0; i < name.Length; i++)
{
if (char.IsWhiteSpace(name[i]))
{
throw new ArgumentException($"Name cannot contain whitespace: \"{name}\"", nameof(name));
}
}

Name = name;
Description = description;
}

private protected override string DefaultName => throw new NotImplementedException();

public override IEnumerable<CompletionItem> GetCompletions(CompletionContext context)
=> Array.Empty<CompletionItem>();
}
}