Simplify building commands by wrapping common functionality in a base class
See original GitHub issueCurrently, when building commands, each command has to include pieces such as calling telemetry, checking if user is connected to a service, retrieving request digest, validating URLs, etc. We could simplify building commands, by wrapping all this into a base class.
Following this suggestion from @erwinvanhunen, I put together a proof of concept showing how this would look in practice. SpoCommand is the base class for all SharePoint Online commands. Notice the implementation of common pieces such as requiresTenantAdmin, logging telemetry or checking connection to SharePoint. Additionally, two methods: getRequestDigest and isValidSharePointUrl are added. With this in place, a command can focus on its specific logic without implementing common pieces.
While the SpoCommand class currently defines the verbose getter, that could go to the Command class so that it’s available to all commands in the CLI. For the PoC I didn’t restructure everything as I just wanted to see how it would work on a few commands.
What do you think: would the gained simplification be worth the effort to refactor the existing commands? @estruyf, @wictorwilen, @andrewconnell
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (7 by maintainers)

Top Related StackOverflow Question
Absolutely valuable. Having this code in all cmdlets makes it complex to change if requirements change later, also it’s pretty error prone (copy and paste errors). I would absolutely move -verbose to the base class, Also think of including parameters (alike PowerShell) for error handling (failsilently, ignore, etc.)
Sorry but I don’t have any CLI examples or have much insight into your questions.