Code Monkey home page Code Monkey logo

Comments (7)

75lb avatar 75lb commented on June 17, 2024

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.

75lb avatar 75lb commented on June 17, 2024

closing until we have reproduction steps and code.

from command-line-args.

blakewest avatar blakewest commented on June 17, 2024

@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.

75lb avatar 75lb commented on June 17, 2024

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.

blakewest avatar blakewest commented on June 17, 2024

@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.

75lb avatar 75lb commented on June 17, 2024

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.

blakewest avatar blakewest commented on June 17, 2024

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)

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.