Nested commands using Mono.Options

Haritha Mohan,mono.optionsCLI

In Part 1 we covered the basics of creating a command line tool using Mono.Options. But what if we wanted to implement a command like this: git branch -m vs. git branch -d

Photo

Technically both commands are git branch but their behaviors and consequently the anatomy of each command are quite different (opens in a new tab). This is where nested commands (opens in a new tab) come into play!

Using the commonality between the 2 commands: branch, this will be the command set and the move and delete will be their individual commands within this set. Create a new directory, BranchCommandSet and a new class BranchCommandSet.cs.

Here, we can use the Mono.Options CommandSet class as a base and establish the commands that will be part of this set.

Photo

If you followed the setup in Part 1 of this series, you should notice building out this functionality should feel like pieces in a puzzle coming together. Like we added the blame command in part 1, we are adding the move and delete commands but within a command set this time. Once the commands are added, you can build out the argument functionality and now we have the flexibility to detail the different arguments and level of requirement specific to each branch.

Don’t forget to add the BranchCommandSet to the set of commands run in the Main method of the program.

Photo

In this post I’ve just mainly highlighted the CommandSet feature but take a look at the repository (opens in a new tab) for more details on implementing arguments.

© Haritha Mohan.RSS