Git Product home page Git Product logo

Comments (5)

ryansch80 avatar ryansch80 commented on May 25, 2024

I'm not sure if this belongs here, or could be considered a separate issue:

I'm using argparse with 3 (required) positional args and 1 optional arg. If you don't pass any args to the program, error is nil and program execution continues (and eventually causes errors when trying to access non existent files):

parser := argparse.NewParser("", "blahblahblah")
var inFile *os.File = parser.FilePositional(os.O_RDONLY, 0600, &argparse.Options{Required: true, Help: "..."})
var outFilename *string = parser.StringPositional(&argparse.Options{Required: true, Help: "..."})
var configFile *os.File = parser.FilePositional(os.O_RDONLY, 0600, &argparse.Options{Required: true, Help: "..."})
startAtFileRow := parser.Int("r", "rowstart", &argparse.Options{Default: 1, Required: false, Help: "..."})

err := parser.Parse(os.Args)
if err != nil {
     log.Fatal(parser.Usage(err))
}

When I push this to a server for use by other users, most of them will naively execute it w/out any args nor even passing "-h" with the expectation that it will show them which arguments are required. In this scenario, the program just explodes:
$ ./asset-migrator
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4b63b1]
...

from argparse.

akamensky avatar akamensky commented on May 25, 2024

@ryansch80 That sounds as a bug that needs to be assessed and fixed.

Yes, that seems to be related, if I understand top post correctly required currently ignored.

As a workaround until this is fixed you could add a check of positional values and throw an error if they are not provided.

from argparse.

akamensky avatar akamensky commented on May 25, 2024

@vsachs Thanks, I think we do need to enable Required to work for positionals. I generally agree with all requirements except for:

Do not throw an error if there are Required positionals on commands which did not Happen

I think if positional is marked as required and there is no value available to fill it -- should throw an error.

I would probably say that as per #108 (comment) it should be considered a bug rather than an enhancement.

from argparse.

vsachs avatar vsachs commented on May 25, 2024

I think if positional is marked as required and there is no value available to fill it -- should throw an error.

If we always throw an error when a required is unsatisfied, then if someone creates multiple sub-commands all of which have a required positional, the argparser will always error. There will be no way for one sub-command to "take precedence". I think that's not a good system but it's up for discussion of course.

it should be considered a bug rather than an enhancement.
Sure, that's fine I guess.

I'll try to take a couple moments next week to fix this. I'm working on Nargs as well but I got pulled away from it.

from argparse.

Outragedpillow avatar Outragedpillow commented on May 25, 2024

I see these issues and comments are older. Are you still maintaining this repo and accepting PRs?

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.