yeoman / generator

Rails-inspired generator system that provides scaffolding for your apps
http://yeoman.io
BSD 2-Clause "Simplified" License
1.2k stars 298 forks source link

Help weirdness #364

Closed nwinkler closed 10 years ago

nwinkler commented 10 years ago

I've noticed a couple of weird things with the --help option:

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: Did not provide required argument name!
    at SubGeneratorGenerator.argument (/usr/local/lib/node_modules/generator-generator/node_modules/yeoman-generator/lib/base.js:211:31)
    at SubGeneratorGenerator.NamedBase (/usr/local/lib/node_modules/generator-generator/node_modules/yeoman-generator/lib/named-base.js:16:8)
    at new SubGeneratorGenerator (/usr/local/lib/node_modules/generator-generator/subgenerator/index.js:6:31)
    at Environment.create (/usr/local/lib/node_modules/yo/node_modules/yeoman-generator/lib/env.js:366:10)
    at Environment.run (/usr/local/lib/node_modules/yo/node_modules/yeoman-generator/lib/env.js:409:24)
    at init (/usr/local/lib/node_modules/yo/bin/yo:98:7)
    at pre (/usr/local/lib/node_modules/yo/bin/yo:110:3)
    at Object.<anonymous> (/usr/local/lib/node_modules/yo/bin/yo:135:1)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

This is rather unexpected. When using the --help option, I shouldn't need to know that I need to provide a required argument. yo generator:subgenerator --help foo works fine, but I find it rather unnecessary that I need to provide the dummy foo argument just to find out the expected arguments and options. Usability would be a lot better if I wouldn't have to know about required parameters if I'm just calling help.

Does that make sense?

nwinkler commented 10 years ago

OK, the first item seems to be fixed by this recent commit: https://github.com/yeoman/generator/commit/4c21283a7b6a8b70235954ad28f02aeb5130f8b3

I'm using version 0.13.4, which doesn't include this commit - so that explains that.

nwinkler commented 10 years ago

The issue with the required attribute and help seems to be a bit of a chicken/egg issue. The problems is caused by the fact that the Base.prototype.argument function both defines the expected argument and also immediately checks for its presence if the argument is defined as required.

If we add a check for the help option before defining the required parameters, we can print the help message, but it won't include the required parameters, since they haven't been defined yet.

The clean way to fix this would be to split up the definition of the parameters and then check for the required parameters later:

  1. Define the parameters, but don't check for presence of required parameters
  2. Check for the presence of the help option. If it's set, print the help message including the definition of the required parameters and then exit.
  3. As a last step, check for required parameters. If there are multiple required parameters missing, print a message listing all of the missing required parameters.
SBoudrias commented 10 years ago

Hi, this have been fixed in this commit: https://github.com/yeoman/generator/commit/4c21283a7b6a8b70235954ad28f02aeb5130f8b3

It should be published with the next yeoman-generator release. Stay tune.