1

I'm currently working on a CLI project using Go and cobra. I've completed the code and now I built the project and trying to run .However, I'm encountering an error that I can't seem to resolve. The error message I'm getting is:

Error: unknown shorthand flag: 'o' in -o

I'm not sure what's causing this error and I'm not sure how to proceed.

Here's the main func:

func main() {
    var rootCmd = &cobra.Command{
        Use:                "Convertica",
        Short:              "Convertica is a file format converter",
    }

    var convCmd = &cobra.Command{
        Use:   "convert",
        Short: "Convert file format",
        Run: func(cmd *cobra.Command, args []string) {
            converter(cmd, args)
        },
    }

    var outCmd = &cobra.Command{
        Use:   "outDir",
        Short: "The directory of the converted file",
        Run: func(cmd *cobra.Command, args []string) {
            content, _ := readContent(cmd, args)
            newName := converter(cmd, args)
            saveContentToDirectory(cmd, content, newName)
        },
    }

    var formatCmd = &cobra.Command{
        Use:   "format",
        Short: "Format of the new file",
        Run: func(cmd *cobra.Command, args []string) {
            converter(cmd, args)
        },
    }

    convCmd.Flags().StringP("file", "c", "", "The directory of the file to be compressed")
    outCmd.Flags().StringP("dir", "o", "", "The directory of the converted file")
    formatCmd.Flags().StringP("format", "f", "", "Format of the new file")

    rootCmd.AddCommand(convCmd)
    rootCmd.AddCommand(outCmd)
    rootCmd.AddCommand(formatCmd)

    if err := rootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}
eglease
  • 2,445
  • 11
  • 18
  • 28
  • Only the sub-command `outDir` has the flag `-o`. It's most likely that you pass this flag to other commands other than `outDir`. Please edit your question to show the full command you run that outputs this error message. – Zeke Lu Aug 19 '23 at 01:30
  • for example => ./convertica conv -c desktop/abc/hey.txt -o desktop/abc/ -f .md – Utku karagül Aug 20 '23 at 20:51

1 Answers1

0

./convertica conv -c desktop/abc/hey.txt -o desktop/abc/ -f .md

That means the sub-command conv has 3 flags: -c, -o and -f. Here is an example showing how to modify your code to do that:

package main

import (
    "fmt"
    "os"

    "github.com/spf13/cobra"
)

func main() {
    var file, dir, format *string
    rootCmd := &cobra.Command{
        Use:   "Convertica",
        Short: "Convertica is a file format converter",
    }

    convCmd := &cobra.Command{
        Use:   "conv",
        Short: "Convert file format",
        Run: func(cmd *cobra.Command, args []string) {
            fmt.Printf("flags:\n\t%v\n\t%v\n\t%v\n", *file, *dir, *format)
        },
    }

    file = convCmd.Flags().StringP("file", "c", "", "The directory of the file to be compressed")
    dir = convCmd.Flags().StringP("dir", "o", "", "The directory of the converted file")
    format = convCmd.Flags().StringP("format", "f", "", "Format of the new file")

    rootCmd.AddCommand(convCmd)

    if err := rootCmd.Execute(); err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
}

See the User Guide for more information.

Zeke Lu
  • 6,349
  • 1
  • 17
  • 23