natemcmaster/CommandLineUtils

[Question] Running code independent of what command handler was executed.

emme1444 opened this issue · 2 comments

A lot of the time when developing CLIs I tend to need some kind of logging configuration or config loading everytime the app starts. Usually, in other languages, CLI creation tools, like these, have a way of invoking every subcommand handler up the path of the subcommand tree for the invoked command. That is, consider the following structure of subcommands:

docker
├─ ps
├─ run
├─ kill
├─ network
│  ├─ ls
│  ├─ create
│  ├─ remove
└─ volume
   ├─ ls
   ├─ create
   └─ remove

Say, from the command line, you execute docker network create [...]. This would then execute the following handler methods (OnExecute*) in the following order: docker -> network -> create (up the tree).

This would be useful, since the handler method for the root docker command would always be executed. Thus, you would be able to configure logging or configuration everytime the app starts. I don't really want to put this before calling CommandLineApplication.Execute*<>() (in Main), since both logging and configuration takes input from the user using the CLI.

I can't really, from what I know, create a base class for the commands which then implements a base implementation for the OnExecute* method to setup logging and configuration, because I wouldn't have access to the CLI options I would need to do so, at that point?

Anyway, I guess my question would be: How would I go about running code independent of what command was executed.

Any help in answering the question would be much appriciated. If you have any thought, please let me know. Thanks!

UPDATE: I use attributes not builder

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.