Skip to content

Implement new combined dartdoc option class #1666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 13, 2018

Conversation

jcollins-g
Copy link
Contributor

@jcollins-g jcollins-g commented Apr 12, 2018

A new class, "DartdocOption", and its subclasses are intended to replace the existing DartDocConfig and DartdocOptions classes. This class automatically handles arguments that can override configuration file options, check for the existence of files, and print useful error messages. It condenses some of the ugly things one typically has to do to make a config object work, so that you can declare them like this:

DartdocOptionSet all_my_flags = new DartdocOptionSet('dartdoc')..addAll([
  new DartdocOptionArgOnly('help', false, abbr: 'h',
      help: 'Displays help for the program'),
  new DartdocOptionBoth<int>('numberOfBugs', 12,
      help: 'How many bugs should dartdoc have for this run'),
  new DartdocOptionBoth<String>('mySpecialFile', null,
      help: 'If you want your special file documented, add it here.',
      mustExist: true, isFile: true),
  new DartdocOptionSet('warning')..addAll([
    new DartdocOptionBoth<bool>('dayIsThursday', false,
        help: 'Emit warning on thursdays.  I could never get the hang of them.',
        negatable: true)
  ]),
]);

This will read a dartdoc_options.yaml file like this:

dartdoc:
  numberOfBugs: 27
  mySpecialFile: "file/needs/to/exist.dart"
  warning:
    dayIsThursday: true

and command line options like this:

dart binary.dart -h
dart binary.dart --number-of-bugs 35 --no-warning-day-is-thursday

overriding the file with the command line options and throwing an assert if files declared that must exist don't, when accessing via:

all_my_files[numberOfBugs].valueAt(Directory.current);

The valueAt allows dartdoc to compute this value for different combinations of config files and arguments, and it should be sufficiently cached to minimize performance hits.

This PR does not actually use this anywhere besides the tests, that's for a followup.

@googlebot googlebot added the cla: yes Google CLA check succeeded. label Apr 12, 2018
@jcollins-g jcollins-g requested review from devoncarew and pq April 12, 2018 21:55
Copy link
Member

@pq pq left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Substantially, 👍.

A few nits and style conversation starters...

import 'package:path/path.dart' as pathLib;
import 'package:yaml/yaml.dart';

import 'logging.dart';

/// Constants to help with type checking, because T is int and so forth
/// don't work in Dart.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

const double _kDoubleVal = 0.0;
const bool _kBoolVal = true;

class DartdocOptionError extends DartDocFailure {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be more consistent to have them both named Dartdoc* or DartDoc*?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed all names to DartdocThing.


// The choice not to use reflection means there's some ugly type checking,
// somewhat more ugly than we'd have to do anyway to automatically convert
// command line arguments and yaml data to real types.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

}

/// A [DartdocOption] that only contains other [DartdocOption]s and is not an option itself.
class DartdocOptionSet extends DartdocOption<Null> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this makes me think the name DartDoc was a typo (meaning Dartdoc instead).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between Dartdoc and DartDoc I prefer Dartdoc, because the command is a single word in my mind. So I've switched them to Dartdoc -- I think it's really a matter of personal preference. I'm interested in consistency though so I've changed them all.

dartdocYaml = pathLib.canonicalize(pathLib.join(
valueWithContext.canonicalDirectoryPath,
valueWithContext.definingFile));
description = "Field ${fieldName} from ${dartdocYaml}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> single quotes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I keep doing this because in bash single quotes mean no resolution of expressions like '${foo}'. I'll try to reprogram myself differently. Fixed.


dartdocOptionSetArgs = new DartdocOptionSet('dartdoc');
dartdocOptionSetArgs
.add(new DartdocOptionArgOnly<bool>('cauliflowerSystem', false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me smile. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-)

dartdocOptionSetBoth.parseArguments(['--my-special-integer', '14']);
expect(
dartdocOptionSetBoth['mySpecialInteger'].valueAt(secondDirFirstSub),
equals(14));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's just a style thing but I prefer to elide explicit calls to equals since I think it defaults to that matcher. In other words, you can just say 14. Totally up to you though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

general dartdoc style is to be explicit in tests, so I've continued that here.

test('validate arg defaults do not override file', () {
dartdocOptionSetBoth.parseArguments([]);
expect(dartdocOptionSetBoth['mySpecialInteger'].valueAt(secondDir),
equals(11));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and below obviously....

() {
dartdocOptionSetBoth.parseArguments(['--my-special-integer', '91']);
expect(dartdocOptionSetBoth['mySpecialInteger'].valueAt(secondDir),
equals(91));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, maybe it actually reads better to be explicit?

expect(
dartdocOptionSetArgs['warn']['unrecognizedVegetable']
.valueAt(tempDir),
equals(false));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> isFalse?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@jcollins-g jcollins-g merged commit d7e6ea5 into master Apr 13, 2018
@jcollins-g jcollins-g deleted the new-dartdoc-option-class branch April 13, 2018 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Google CLA check succeeded.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants