Skip to content

Commit

Permalink
feat: Improve command completion
Browse files Browse the repository at this point in the history
  • Loading branch information
caffeine-addictt committed Apr 13, 2024
1 parent d766428 commit 8a44a28
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
13 changes: 7 additions & 6 deletions src/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,17 +265,18 @@ func init() {
getFlags.strategy = strategyConcurrent

rootCommand.AddCommand(getCommand)
getCommand.Flags().StringVarP(&getFlags.inputFile, "file", "f", "", "Path to the input file containing the url(s)")
getCommand.Flags().IntVarP(&getFlags.maxConcurrency, "max-concurrency", "m", 10, "Maximum number of concurrent downloads [0 = unlimited] (default is 10)")
getCommand.Flags().VarP(&getFlags.strategy, "strategy", "s", "Strategy to use when downloading (default is concurrent)")
if err := getCommand.RegisterFlagCompletionFunc("strategy", strategyCompletion); err != nil {
fmt.Println("Failed to register completion for flag -s in get command")

getCommand.Flags().StringVarP(&getFlags.inputFile, "file", "f", "", "Path to the input file containing the url(s)")
if err := getCommand.MarkFlagFilename("file"); err != nil {
fmt.Println("Failed to mark flag -f as filename in get command")
Debug(err.Error())
os.Exit(1)
}

if err := getCommand.MarkFlagFilename("file"); err != nil {
fmt.Println("Failed to mark flag -f as filename in get command")
getCommand.Flags().VarP(&getFlags.strategy, "strategy", "s", "Strategy to use when downloading (default is concurrent)")
if err := getCommand.RegisterFlagCompletionFunc("strategy", strategyCompletion); err != nil {
fmt.Println("Failed to register completion for flag -s in get command")
Debug(err.Error())
os.Exit(1)
}
Expand Down
19 changes: 18 additions & 1 deletion src/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,20 @@ func init() {
// Configuration
func init() {
cobra.OnInitialize(initConfig)

rootCommand.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is $HOME/.video-manager)")
if err := rootCommand.MarkPersistentFlagFilename("config"); err != nil {
fmt.Println("Failed to mark flag -c as filename in root command")
Debug(err.Error())
os.Exit(1)
}

rootCommand.PersistentFlags().StringVarP(&cacheFile, "cache", "C", "", "cache file (default is $HOME/.video-manager_history)")
if err := rootCommand.MarkPersistentFlagFilename("cache"); err != nil {
fmt.Println("Failed to mark flag -C as filename in root command")
Debug(err.Error())
os.Exit(1)
}
viper.SetDefault("cache", filepath.Clean(filepath.Join(home, ".video-manager_history")))
if err := viper.BindPFlag("cache", rootCommand.PersistentFlags().Lookup("cache")); err != nil {
fmt.Println("Failed to bind persistent flag 'cache'")
Expand All @@ -66,12 +78,17 @@ func init() {

// Working directory
rootCommand.PersistentFlags().StringVarP(&workingDir, "dir", "w", "~/Videos", "Working directory (default is ~/Videos)")
if err := rootCommand.MarkPersistentFlagDirname("dir"); err != nil {
fmt.Println("Failed to mark flag -w as dirname in root command")
Debug(err.Error())
os.Exit(1)
}
viper.SetDefault("dir", "~/Videos")
if err := viper.BindPFlag("dir", rootCommand.PersistentFlags().Lookup("dir")); err != nil {
fmt.Println("Failed to bind persistent flag 'dir'")
Debug(err.Error())
os.Exit(1)
}
viper.SetDefault("dir", "~/Videos")
}

func initConfig() {
Expand Down

0 comments on commit 8a44a28

Please sign in to comment.