Nesting commands

You can nest commands. Let's take git for example

git has a command called stash. When you execute git stash, it stashes all the changes. But stash has further commands like, git stash pop, git stash list, etc.

Let's try and mimic the same behavior using CommandDotNet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[ApplicationMetadata(Description = "Fake git application")]
public class Git
{
    [ApplicationMetadata(Description = "Commits all staged changes")]
    public void Commit([Option(ShortName = "m")]string commitMessage)
    {
        Console.WriteLine("Commit successful");
    }

    [ApplicationMetadata(Description = "Stashes all changes when executed without any arguments")]
    [SubCommand]
    public class Stash
    {
        [DefaultMethod]
        public void StashDefaultCommand()
        {
            Console.WriteLine($"changes stashed");
        }

        [ApplicationMetadata(Description = "Applies last stashed changes")]
        public void Pop()
        {
            Console.WriteLine($"stash popped");
        }

        [ApplicationMetadata(Description = "Lists all stashed changes")]
        public void List()
        {
            Console.WriteLine($"here's the list of stash");
        }
    }
}

Here's how the help looks like now:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Fake git application

Usage: dotnet example.dll [options] [command]

Options:
  -h | -? | --help  Show help information

Commands:
  Commit  Commits all staged changes
  Stash   Stashes all changes when executed without any arguments

Use "dotnet example.dll [command] --help" for more information about a command.

Here's how the interaction looks like:

INPUT

1
dotnet example.dll commit -m "some refactoring"

OUTPUT

1
Commit successful

INPUT

1
dotnet example.dll stash

OUTPUT

1
changes stashed

INPUT

1
dotnet example.dll stash --help

OUTPUT

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Stashes all changes when executed without any arguments

Usage: dotnet example.dll Stash [options] [command]

Options:
  -h | -? | --help  Show help information

Commands:
  List  Lists all saved stashed changes
  Pop   Applies last stashed changes

Use "Stash [command] --help" for more information about a command.

INPUT

1
dotnet example.dll stash pop

OUTPUT

1
stash popped

Alternative

If you like to store your sub commands as external .cs files, you can that too with [SubCommand] attribute.

1
2
3
4
5
6
7
8
9
[ApplicationMetadata(Description = "Stashes all changes when executed without any arguments")]
public class Stash
{
    [ApplicationMetadata(Description = "Applies last stashed changes")]
    public void Pop()
    {
        Console.WriteLine($"stash popped");
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
[ApplicationMetadata(Description = "Fake git application")]
public class Git
{
    [SubCommand]
    public Stash Stash {get;set;} // Stash class is saved in a seperate file

    [ApplicationMetadata(Description = "Commits all staged changes")]
    public void Commit([Option(ShortName = "m")]string commitMessage)
    {
        Console.WriteLine("Commit successful");
    }
}

Last update: February 22, 2020