Comments (5)
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.
@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.
@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.
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.
I see these issues and comments are older. Are you still maintaining this repo and accepting PRs?
from argparse.
Related Issues (20)
- How do I make a hidden argument? HOT 1
- SelectorList flag HOT 2
- Golang Argparse not picking correct value for multiple flags HOT 1
- Allow parser.ParseKnownArgs(...) HOT 2
- How do you get >=0 positionals? HOT 2
- [BUG] v1.4.0 args does not happened when subcommand exists
- Positional args HOT 2
- Creating commands in a type HOT 1
- Add compatibility with complete library [ bash completions ] HOT 1
- Auto complete subcommands HOT 1
- [bug] Unable to detect if file flag was actually provided? HOT 7
- subparser not implemented HOT 1
- Provide ability to configure helptext maxWidth columns HOT 9
- Getting: import cycle not allowed using the example. HOT 1
- Provide <META> fields and Author Information in usage string HOT 2
- Question about returned parameters (not a bug report) HOT 1
- FlagCounter display unexpectedly HOT 1
- expect File arg to take standard input / output stream as a value HOT 2
- Options.Default should be ignored for Flag HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from argparse.