Coder Social home page Coder Social logo

Comments (11)

jhughes1153 avatar jhughes1153 commented on August 26, 2024

I'd be interested in helping with this one if possible.

from argparse.

akamensky avatar akamensky commented on August 26, 2024

Feel free to pick this up! I think can start with adding relevant test case for tests.

from argparse.

jhughes1153 avatar jhughes1153 commented on August 26, 2024

Im not getting an error message like I would expect. Is this just referring to the '[--arg] is required' and then the help message being dumped after that or is it something else? Im have trouble reproducing an error with subcommands.

from argparse.

akamensky avatar akamensky commented on August 26, 2024

@jhughes1153 I've put together following example:

package main

import (
	"fmt"
	"github.com/akamensky/argparse"
	"os"
)


func main() {
	p := argparse.NewParser("example", "simple sub-command example")
	
	subOne := p.NewCommand("sub-one", "Sub-command one")
	subTwo := p.NewCommand("sub-two", "Sub-command two")

	intArgOne := subOne.Int("i", "int", &argparse.Options{
		Required: true,
		Help: "simple integer",
	})

	intArgTwo := subTwo.Int("i", "int", &argparse.Options{
		Required: true,
		Help: "simple integer",
	})

	err := p.Parse(os.Args)
	if err != nil {
		panic(err)
	}

	if subOne.Happened() {
		fmt.Printf("sub-command one arg is %d\n", *intArgOne)
	} else if subTwo.Happened() {
		fmt.Printf("sub-command two arg is %d\n", *intArgTwo)
	}
}

the output running this is as follows:

$ go run main.go --help
usage: example <Command> [-h|--help]

               simple sub-command example

Commands:

  sub-one  Sub-command one
  sub-two  Sub-command two

Arguments:

  -h  --help  Print help information


$ go run main.go sub-one --help
panic: [-i|--int] is required

goroutine 1 [running]:
main.main()
	/tmp/main.go:28 +0x603
exit status 2

As you can see on second run it did not print help message as requested, but instead it returned error that [-i|--int] is required, while i believe it should have printed help message for sub-command. If I change Required: false and add Default: 0 to sub-commands then it works as expected:

$ go run main.go sub-one --help
usage: example sub-one [-i|--int <integer>] [-h|--help]

               Sub-command one

Arguments:

  -i  --int   simple integer. Default: 0
  -h  --help  Print help information

from argparse.

akamensky avatar akamensky commented on August 26, 2024

I believe -h|--help should have higher priority than required:true arguments.

from argparse.

jhughes1153 avatar jhughes1153 commented on August 26, 2024

Thanks for the explanation, I was trying to use the zooprog example and I guess I was just doing something wrong with trying to produce the error.

from argparse.

jhughes1153 avatar jhughes1153 commented on August 26, 2024

So it looks like help isnt caught in the sub commands because the sub commands dont look for the --help so they just panic on the required, so I guess we could either add help as a special flag to all subcommands or change that when it parses the flags and sees help itll call the most recent help function. Ill look into what python argparse does in this case for reference as well.

from argparse.

jhughes1153 avatar jhughes1153 commented on August 26, 2024

It looks like in argparse for python they just add help to each subparser so I think ill head in that direction first

from argparse.

jhughes1153 avatar jhughes1153 commented on August 26, 2024

@akamensky when you disable help on the parser should it disable the help for all subcommands as well?

from argparse.

jhughes1153 avatar jhughes1153 commented on August 26, 2024

Sorry for taking so long with this, I still want to work on this bug but my job has been crazy lately and taking up a lot of time on my weekends, but these next few weekends I should be free to work on it so i'll be able to spend some time on this

from argparse.

akamensky avatar akamensky commented on August 26, 2024

Closed by #72

from argparse.

Related Issues (20)

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.