Skip to content

Instantly share code, notes, and snippets.

@terrajobst
Last active September 29, 2015 15:30
Show Gist options
  • Save terrajobst/2ed5fcaa97f533095d97 to your computer and use it in GitHub Desktop.
Save terrajobst/2ed5fcaa97f533095d97 to your computer and use it in GitHub Desktop.
System.CommandLine with commands

For simple commands, it's probably easiest to do a switch:

static class Program
{
    static void Main(string[] args)
    {
        var command = string.Empty;
        var fileName = string.Empty;
        var keepExisting = false;

        ArgumentSyntax.Parse(args, syntax =>
        {
            syntax.DefineCommand("import", ref command, "Import rows");
            syntax.DefineOption("k|keep-existing", ref keepExisting, "Keep existing rows");
            syntax.DefineParameter("path", ref fileName, "File to import");

            syntax.DefineCommand("export", ref command, "Export rows");
            syntax.DefineParameter("path", ref fileName, "File to export to");
        });

        switch (command)
        {
            case "import":
                Import(fileName, keepExisting);
                break;
            case "export":
                Export(filename);
                break;
        }
    }

    static void Import(string fileName, bool keepExisting)
    {
        //... 
    }

    static void Export(string fileName)
    {
        //... 
    }
}

For more complicated cases it may be worthwhile to extract the commands into separate types:

static class Program
{
    static void Main(string[] args)
    {
        var commands =
        {
            new ImportCommand();
            new ExportCommand();
        };

        var result = ArgumentSyntax.Parse(args, syntax =>
        {
            foreach (var c in commands)
                c.Define(syntax);
        });

        var command = (Command)result.ActiveCommand.Value;
        command.Execute();
    }

    abstract class Command
    {
        public abstract string Name { get; }
        public abstract string Help { get; }
        public abstract void Define(ArgumentSyntax syntax);
        public abstract void Execute();
    }

    class ImportCommand : Command
    {
        string _keepExisting;
        string _fileName;

        public override void Define(ArgumentSyntax syntax)
        {
            syntax.DefineCommand("import", this, "Import rows");
            syntax.DefineOption("k|keep-existing", ref _keepExisting, "Keep existing rows");
            syntax.DefineParameter("path", ref _fileName, "File to import");
        }

        public void Execute()
        {
            // ...
        }
    }

    class ExportCommand : Command
    {
        string _fileName;
        
        public override void Define(ArgumentSyntax syntax)
        {
            syntax.DefineCommand("export", this, "Export rows");
            syntax.DefineParameter("path", ref _fileName, "File to export to");
        }
 
        public void Execute()
        {
            // ...
        }
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment