build_cli 0.2.0 copy "build_cli: ^0.2.0" to clipboard
build_cli: ^0.2.0 copied to clipboard

outdatedDart 1 only

Parse command line arguments directly into an annotation class using the power of build_runner and source_gen.

Build Status

Parse command line arguments directly into an annotation class using the Dart Build System.

Example #

Annotate a class with @CliOptions() from package:build_cli_annotations.

import 'package:build_cli_annotations/build_cli_annotations.dart';

part 'example.g.dart';

@CliOptions()
class Options {
  @CliOption(abbr: 'n', help: 'Required. The name to use in the greeting.')
  final String name;

  final bool nameWasParsed;

  bool yell;

  @CliOption(defaultsTo: Language.en, abbr: 'l')
  Language displayLanguage;

  @CliOption(negatable: false, help: 'Prints usage information.')
  bool help;

  Options(this.name, {this.nameWasParsed});
}

enum Language { en, es }

Configure and run the Dart Build System and a set of helpers is created to parse the corresponding command line arguments and populate your class.

void main(List<String> args) {
  var options = parseOptions(args);
  if (!options.nameWasParsed) {
    throw new ArgumentError('You must set `name`.');
  }
  print(options.name);
}

Setup #

Add three packages to pubspec.yaml:

dependencies:
  build_cli_annotations: ^0.1.0

dev_dependencies:
  build_cli: ^0.2.0
  build_runner: '>=0.7.10 <0.9.0'
  • build_cli_annotations is a separate package containing the annotations you add to classes and members to tell build_cli what to do.
    • If the code you're annotating is in a published directory – lib, bin – put it in the dependencies section.
  • build_cli contains the logic to generate the code.
    • It should almost always be put in dev_dependencies.
  • build_runner contains the logic to run a build and generate code.
    • It should almost always be put in dev_dependencies.

Details #

Uses package:args under the covers.

More examples: #

14
likes
0
pub points
64%
popularity

Publisher

verified publisherj832.com

Parse command line arguments directly into an annotation class using the power of build_runner and source_gen.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

analyzer, build, build_cli_annotations, build_config, meta, pub_semver, source_gen, yaml

More

Packages that depend on build_cli