feedhenry / fh-fhc

FeedHenry CLI, the Command Line Interface to FeedHenry
Other
26 stars 66 forks source link

Argument parsing fails if using as a module #383

Closed evanshortiss closed 6 years ago

evanshortiss commented 7 years ago

I'm working on a PR that caused me to run into this issue. When using fhc as a CLI the following works fine:

fhc projects read --project=rmrqnkjpfjnpqmu6ktanlgy7

Using it as a module:

fhc.applyCommandFunction('projects read --project=rmrqnkjpfjnpqmu6ktanlgy7', callback)

causes it to return an error:

Usage:
 fhc projects read --project=<project>

Examples:
  fhc projects read --project=<project>    Read the <project>

Options:
  --project, -p  Unique 24 character GUID of the project  [required]

Missing required arguments: project
Unhandled rejection Error: Missing required arguments: project
    at fail (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/fh-fhc/node_modules/yargs/index.js:192:21)
    at parseArgs (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/fh-fhc/node_modules/yargs/index.js:537:13)
    at Object.Argv.Object.defineProperty.get [as argv] (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/fh-fhc/node_modules/yargs/index.js:460:36)
    at Object.exports.validate (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/fh-fhc/lib/utils/args.js:23:15)
    at EventEmitter.fhc.applyCommandFunction (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/fh-fhc/lib/fhc.js:158:23)
    at EventEmitter.tryCatcher (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/bluebird/js/release/util.js:16:23)
    at EventEmitter.ret [as applyCommandFunctionAsync] (eval at <anonymous> (/Users/eshortis/workspaces/rhmap-features/rhmap-previewer-cloud/node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:13:39)

I believe I have tracked it down to the _normaliseFlagsAndArs in args.js. This function incorrectly assumes that FHC will be used as a CLI only and uses process.argv instead of the argsIntoCommand variable which should be used (as far as I can tell).

camilamacedo86 commented 6 years ago

Hi @evanshortiss,

It is not the way expected to use it as a lib. It was described in https://github.com/feedhenry/fh-fhc/pull/384 and in the README. Following an example of fhc used as a dependency.

 fhc.env.list({app:'hgxe4z5j2b6owln5vw6qvx5i', env:'dev', json : true }, function (err,res) {
      return cb( res);
    });

For further information check the README here.

Please, let me know if you still facing issues or have any other concern.

Cheers, Camila Macedo