Skip to content

Commit

Permalink
feat: show available commands when 0 or 1 required arguments are given
Browse files Browse the repository at this point in the history
  • Loading branch information
flawiddsouza committed Aug 13, 2022
1 parent ccb2349 commit b950bd8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ goreleaser release --rm-dist

## Installing on Linux (tested on Ubuntu)
```bash
wget https://github.com/flawiddsouza/shortcommand/releases/download/v0.0.2/shortcommand_0.0.2_Linux_x86_64.tar.gz
tar -xf shortcommand_0.0.2_Linux_x86_64.tar.gz
wget https://github.com/flawiddsouza/shortcommand/releases/download/v0.0.3/shortcommand_0.0.3_Linux_x86_64.tar.gz
tar -xf shortcommand_0.0.3_Linux_x86_64.tar.gz
mv shortcommand ~/.local/bin
rm shortcommand_0.0.2_Linux_x86_64.tar.gz
rm shortcommand_0.0.3_Linux_x86_64.tar.gz
```
You should then be able to use the `shortcommand` command anywhere you are.
29 changes: 24 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@ func main() {
return
}

if len(os.Args) < 3 {
fmt.Println("Two arguments need to passed to the command, first is your shortcommand and then the command under it")
return
}

var parsedConfig Config

err = yaml.Unmarshal(yamlFile, &parsedConfig)
Expand All @@ -98,6 +93,30 @@ func main() {
return
}

if len(os.Args) < 2 {
fmt.Println("Available commands")
for _, shortCommand := range parsedConfig.ShortCommands {
fmt.Printf("%s\n", shortCommand.Name)
for _, command := range shortCommand.Commands {
fmt.Printf("%s%s%s%s\n", " ", command.Name, "\t", command.Description)
}
}
return
}

if len(os.Args) < 3 {
for _, shortCommand := range parsedConfig.ShortCommands {
if shortCommand.Name == os.Args[1] {
fmt.Printf("Available sub commands for %s\n", os.Args[1])
for _, command := range shortCommand.Commands {
fmt.Printf("%s%s%s\n", command.Name, "\t", command.Description)
}
break
}
}
return
}

for _, shortCommand := range parsedConfig.ShortCommands {
if shortCommand.Name == os.Args[1] {
for _, command := range shortCommand.Commands {
Expand Down

0 comments on commit b950bd8

Please sign in to comment.