Skip to content

Commit

Permalink
Dynamic command completion on the volume.
Browse files Browse the repository at this point in the history
  • Loading branch information
hilli committed May 18, 2023
1 parent 7289b46 commit f4049ac
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions cmd/kefw2/cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var volumeCmd = &cobra.Command{
Use: "volume",
Short: "Get or adjust the volume of the speakers",
Long: `Get or adjust the volume of the speakers`,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
volume, _ := currentSpeaker.GetVolume()
Expand All @@ -29,20 +30,11 @@ var volumeCmd = &cobra.Command{
os.Exit(1)
}
},
ValidArgsFunction: VolumeCompletion,
}

func init() {
rootCmd.AddCommand(volumeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// volumeCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// volumeCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func parseVolume(volume string) (int, error) {
Expand All @@ -56,3 +48,7 @@ func parseVolume(volume string) (int, error) {
}
return v, nil
}

func VolumeCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"0", "10", "20", "30", "40", "50", "60", "70", "80", "90", "100"}, cobra.ShellCompDirectiveNoFileComp
}

0 comments on commit f4049ac

Please sign in to comment.