Comments (7)
Hi! To help me reproduce and fix this quickly, please supply some source - either real source or a simple test case.. paste it here or in a gist.. cheers!
from command-line-args.
closing until we have reproduction steps and code.
from command-line-args.
@75lb hey there, sorry for delay. I just put together an example repo reproducing the bug: https://github.com/blakewest/commandlineargsdemo
Hope that helps. Let me know if you have questions. Thanks. As a side note, I eventually decided to switch to a different command line args library because of this bug. But I otherwise thought your library was better, because it has more options, and a clearer syntax.
from command-line-args.
thanks for the test case - very useful! I was able to track the issue within a few minutes.
What you are seeing is the expected behaviour. You are parsing the command line twice - once in script A, once in script B. So, when you run this command:
$ node scriptA.js --withHelper
it is accepted by script A (where it has a definition) but not script B (where it does not have a definition).
Correct behaviour.
from command-line-args.
@75lb thanks for checking it out, and it sounds like you get the situation. The behavior seems, if nothing else, unexpected. If it is expected, the error message could be clearer. It took me a while to realize what was even happening. As I run that line, I'm thinking, "I'm running scriptA with a valid argument to script A. Why does it think the argument is unknown?". And I would not expect that the command line args would get validated against files that are merely getting required, and not run.
But aside from that, just consider the use case that the repo replicates, which is... "Sometimes I run script A, and sometimes I run script B. Both have their own command line args, but they also share code amongst one another." Under the current library version, this is not possible without some very odd hacks. Assuming this use case seems valid, what is the work around?
from command-line-args.
every time you run commandLineArgs(definitions)
(from one module, from multiple modules, from wherever) the command line arguments (in the global process.argv
array) are parsed using the definitions provided.
You are parsing the command line twice in the same synchronous tick. If either of those invocations are invalid you'll get errors. Maybe passing your own argv
array to commandLineArgs()
or using partial: true
would help. Check the docs.
from command-line-args.
A workaround I just tested out is to do something like this...
// Only parse command line args if you're calling this file from the command line.
// eg. this file is scriptA, but we call scriptB from the command line, and scriptB requires scriptA.
// This ensures we don't run scriptA's command line args parsing, which could throw an error
// that we don't care about.
if (process.argv[1] !== __filename) {
commandLineArgs(myOptions)
}
from command-line-args.
Related Issues (20)
- Throw an exception if both `lazyMultiple` and `multiple` are set HOT 3
- Aliases should be nullable HOT 2
- Support for negated flags HOT 8
- recommended way to handle help? HOT 1
- Allow configuring arguments to be required HOT 3
- When trying to use a proxy such as jFrog's Artifactory, npm install fails HOT 2
- Missing argument should be error? HOT 1
- How to set a boolean value that defaults to true to false. HOT 1
- Multiple as a single type? HOT 2
- Jumping to [email protected] restrict node to >=14 HOT 2
- double hyphen is parsed as an argument when `defaultOption` is set
- Impossible to validate missing arguments for a flag when `multiple` is set HOT 1
- Parsing error of strings that starts with a digit HOT 1
- name containing dash (e.g. --num-max) supported? HOT 2
- Support concatenated .multiple for string arguments HOT 2
- Unclear behavior with defaultOption and more-than-one multiple args HOT 5
- docs for Usage guide generation is missing HOT 2
- New feature: Add a way to specify boolean value as an argument HOT 2
- What exactly is the purpose of defaultOption in multiple? HOT 1
- Support required options HOT 1
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 command-line-args.