Prevent registering homonymous commands

Open Beginner friendly
#32 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
62/100
Issue type
Feature
Clarity
Clearly specified
Activity status
Stale
Tech stack
go
Domain
cli

Research direction

Start in subcommands.go at the Register behavior referenced near line 199, then inspect how subcommands.Commander stores and selects commands. Verify how repeated registrations with the same name behave. Done means a later registration replaces the earlier homonymous command instead of leaving the first one selected.

Written by the indexing model from the issue text.

Description

I could find this behavior when using this library in conjunction with go-cmdtest.

Because of how test suites in go-cmdtest work, a subcommand can be tested several times with different flags and arguments. In order for this to happen, for each variation, the program's main function is called, what consists of registering commands, so subcommands.Register is called more than once for the same command in a single go test run.

Because go-cmdtest overrides os.Stdout and os.Stderr before each test, I correctly set them to my type that implements subcommands.Command just before registering it, so it points to the correct output set by go-cmdtest.

However, because I register commands with the same name more than once and they are stored in the same subcommands.Commander (subcommands.DefaultCommander), when executing the subcommand more than once, only the first one registered is picked.

Of course, I already solved my problem by creating a new subcommands.Commander in the main function, but if a subcommands.Commander can only execute the first command registered for a list of homonymous commands, there is no point of letting more than one homonym to be registered.

So, for fixing this, I think there is no point in discarding the newer ones. I guess the best solution would be to override a subcommand when another of same name already exists. This way, no unused homonyms will get appended.

Dominant language
Go
Stars
794
Forks
52
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/subcommands

All issues in google/subcommands

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.