GithubHelp home page GithubHelp logo

hedzr / cmdr Goto Github PK

View Code? Open in Web Editor NEW
133.0 5.0 9.0 2.43 MB

POSIX-compliant command-line UI (CLI) parser and Hierarchical-configuration operations

Home Page: https://hedzr.com/cmdr-docs/

License: Apache License 2.0

Go 99.14% Shell 0.18% Dockerfile 0.65% Makefile 0.03%
command-line-parser command-line-interface commandline-interface commandlineparser commandline-arguments commandline command-line cli getopt hierarchy-configurations

cmdr's Issues

ToggleGroup & flag.Args()

Hi,
Maybe I'm missing what the feature ToggleGroup is but I thought that by setting a values like this would make the values mutually exclusive

                {
                        BaseOpt: cmdr.BaseOpt{
                                Short:       "L",
                                Full:        "list",
                                Description: "list command",
                                Group:       cStartup,
                        },
                        DefaultValue: false,
                        ToggleGroup:  "target",
                },
                {
                        BaseOpt: cmdr.BaseOpt{
                                Short:       "G",
                                Full:        "grain",
                                Description: "grain command",
                                Group:       cStartup,
                        },
                        DefaultValue: false,
                        ToggleGroup:  "target",
                },

but I can run both commands and I don't get an error.

Also, I would have wanted to have the option to get the remaining args that were not parsed just as in the official flag package.

双引号的问题

const (
	versionName = "0.0.1"
	appName     = "cmdrtest"
)

func buildRootCmd() (rootCmd *cmdr.RootCommand) {

	root := cmdr.Root(appName, versionName)

	root.NewFlag(cmdr.OptFlagTypeString).
		Titles("o", "output-file").
		Description("output file", "").
		DefaultValue("", "").
		OnSet(func(keyPath string, value interface{}) {
			fmt.Println(keyPath)
			os.Exit(0)
			return
		})

	rootCmd = root.RootCommand()

	return
}

func main() {

	rootCmd := buildRootCmd()
	if err := cmdr.Exec(rootCmd,
		cmdr.WithBuiltinCommands(false, false, false, false, false),
	); err != nil {
		panic(err)
	}
}

编译完如果 cmdrtest -o "" 就会 panic

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
github.com/hedzr/cmdr.(*ptpkg).preprocessPkg(0xc0002c6000, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x5)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/ptpkg.go:170 +0x33a
github.com/hedzr/cmdr.(*ptpkg).processTypeString(0xc0002c6000, 0xc00002a0c0, 0x3, 0x4, 0xffffffffffffffff, 0xc0000220d1)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/ptpkg.go:280 +0x6e
github.com/hedzr/cmdr.(*ptpkg).tryExtractingValue(0xc0002c6000, 0xc00002a0c0, 0x3, 0x4, 0xc0002784e8, 0x9e509f)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/ptpkg.go:100 +0x112
github.com/hedzr/cmdr.(*ExecWorker).flagsMatched(0xc0002b8120, 0xc0002c6000, 0xc0002981c0, 0xc00002a0c0, 0x3, 0x4, 0x9c8ede, 0xc0002c6000, 0xc0002c6028)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec_match.go:133 +0x5f
github.com/hedzr/cmdr.(*ExecWorker).flagsMatching(0xc0002b8120, 0xc0002c6000, 0xc0002981c0, 0xc00022be48, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x0, 0x0)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec_match.go:99 +0x3ce
github.com/hedzr/cmdr.(*ExecWorker).xxTestCmd(0xc0002b8120, 0xc0002c6000, 0xc00022be48, 0xc0002981c0, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x1, 0xc000134060)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec.go:244 +0x15c
github.com/hedzr/cmdr.(*ExecWorker).InternalExecFor(0xc0002b8120, 0xc0002981c0, 0xc00002a0c0, 0x3, 0x4, 0x0, 0x0)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec.go:194 +0x2da
github.com/hedzr/cmdr.Exec(0xc0002981c0, 0xc00022bf38, 0x1, 0x1, 0x43e6c1, 0x1081ec0)
        $GOPATH/pkg/mod/github.com/hedzr/[email protected]/exec.go:88 +0x9e
main.main()

单引号的话就没问题~

Gallery

wget-demo app

image

demo app

image

man page

# generate man pages to `./man1`
mkdir man1 && bin/demo gen man
# move them to system location:
mv ./man1/* /usr/local/man/man1/
# And, query them via `man 1 demo` or `man 1 demo server`:
man demo-generate

And:
image

禁用 help 之后

谢谢你的作品,很棒的库,流式调用链相当舒服,使用中遇到了一个疑问:

const (
	versionName = "0.0.1"
	appName     = "cmdrtest"
)

func buildRootCmd() (rootCmd *cmdr.RootCommand) {

	root := cmdr.Root(appName, versionName)

	// root.NewSubCommand().
	// 	Titles("h", "help").
	// 	Description("show help screen", "").
	// 	Action(func(cmd *cmdr.Command, args []string) (err error) {
	// 		fmt.Println("this is help text")
	// 		os.Exit(0)
	// 		return
	// 	})

	// root.NewFlag(cmdr.OptFlagTypeBool).
	// 	Titles("h", "help").
	// 	Description("show help screen", "").
	// 	DefaultValue(false, "").
	// 	OnSet(func(keyPath string, value interface{}) {
	// 		fmt.Println("this is help text")
	// 		os.Exit(0)
	// 		return
	// 	})

	rootCmd = root.RootCommand()

	return
}
func main() {

	// To disable internal commands and flags, uncomment the following codes
	cmdr.EnableVersionCommands = false
	cmdr.EnableVerboseCommands = false
	cmdr.EnableHelpCommands = false
	cmdr.EnableGenerateCommands = false
	cmdr.EnableCmdrCommands = false

	rootCmd := buildRootCmd()
	if err := cmdr.Exec(rootCmd); err != nil {
		panic(err)
	}
}

这个时候 -h 或者 --help 似乎是无效的 (Unknown flag: -h)

但是直接运行会默认输出 usage,最后一行仍然会是:

Type '-h' or '--help' to get command help screen.

如果想要实现它,用 NewSubCommand 的话会不能带 - 符号,就和最后一行的输出相矛盾了。此时该用 NewFlag(cmdr.OptFlagTypeBool)OnSet 吗?但总感觉它应该是 Command 范畴,因为可能需要实现 go help build 这种效果

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.