question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

commands/subcommands API is awkward

See original GitHub issue

Is your feature request related to a problem? Please describe.

Building a CLI tool that takes has multiple commands (verbs) when processing a given data store.

manage-store add <add params>
manage-store list <list param>
manage-store delete <delete params>

Describe the solution you’d like

I’d like to just define the add, list and delete classes decorated with something like the [Command] (?) attribute. And perhaps a single entrypoint into a library-standardized dispatcher e.g.

public static void Main(string[] args) 
{
    // making up this name but you get the point
    McMaster.Extensions.CommandLineUtils.RouteToCommand(args);
}

Describe alternatives you’ve considered

The current documentation just feels very awkward. It seems to offload CLI parsing and routing/dispatching back to user written code. Given that the concern is CLI parsing and routing, this should be handled from within the library itself.

On a related note, the documentation is unnecessarily complicated by cramming too many things into a single command/sub-command example.

IMHO, your root level readme.md should cover one quick example for regular CLIs (e.g. wget) and another example for verbs/commands based CLIs (like dotnet <verbs>).

Additional context

Don’t really care about the builder API if it matters. Great tool by the way - thanks!

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
SidShetyecommented, Jul 4, 2018

@natemcmaster seems like you already have this functionality and it’s actually quite simple and well designed. Documentation is definitely misleading/confusing about the minimal ‘magic’ / assumptions needed to get this working.

Program.cs

using McMaster.Extensions.CommandLineUtils;

namespace DataStore
{
    [Command(ThrowOnUnexpectedArgument = false)]
    [Subcommand("add", typeof(AddCmd))]
    [Subcommand("delete", typeof(DeleteCmd))]
    [HelpOption("--help")]
    class Program
    {
        public static void Main(string[] args)
        { 
            CommandLineApplication.Execute<Program>(args);
        }
    }
}

AddCmd.cs (DeleteCmd is similar)

using McMaster.Extensions.CommandLineUtils;
using System;
using System.ComponentModel.DataAnnotations;

namespace DataStore
{
    [HelpOption("--help")]
    public class AddCmd
    {
        [Required]
        [Option("--addParam1", Description = "1st parameter for add")]
        public string AddParam1 { get; set; }

        [Required]
        [Option("--addParam2", Description = "2nd parameter for add")]
        public MyEnum AddParam2 { get; set; }

        public void OnExecute()
        {
            // do work, AddParam1 and AddParam2 (enum auto-parsed) available here
        }
    }
}

Regarding System.Commandline that would be even better. IMHO as long as ‘console application’ is a supported flavor, this sort of CLI parsing should be right in the .net standard itself. Imagine having ASP.NET as a supported programming model but leaving Controllers or the router entirely as community effort libraries. So your work is very much appreciated!

0reactions
stale[bot]commented, Apr 6, 2019

This issue has been automatically marked as stale because it has not had 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 7 days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with subcommands: ctx.invoked_subcommands is ...
1 Answer 1 · I just tried your suggestion, but it still has the same behavior: whether i use a subcommand or not,...
Read more >
Discord.js Subcommand Handler - YouTube
Code: https://github.com/stuyy/slash- command -handler-discord.js-v14 Support the Channel: Become a Member: ...
Read more >
API — Click Documentation (8.1.x)
This part of the documentation lists the full API reference of all public classes and ... Creates a new Command and uses the...
Read more >
argparse — Parser for command-line options, arguments ...
This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial....
Read more >
poise - Rust
Subcommands. Commands in poise have a tree structure. Every commands refers to a list of subcommands, which you can easily set using the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found