Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kevmoo/build_cli

A builder that generates an ArgsParser from a class
https://github.com/kevmoo/build_cli

code-generation command-line-parser dart dart-build-system

Last synced: about 2 months ago
JSON representation

A builder that generates an ArgsParser from a class

Awesome Lists containing this project

README

        

[![Dart CI](https://github.com/kevmoo/build_cli/actions/workflows/dart.yml/badge.svg?branch=master)](https://github.com/kevmoo/build_cli/actions/workflows/dart.yml?query=workflow%3Adart+branch%3Amaster)
[![Pub package](https://img.shields.io/pub/v/build_cli.svg)](https://pub.dev/packages/build_cli)
[![package publisher](https://img.shields.io/pub/publisher/build_cli.svg)](https://pub.dev/packages/build_cli/publisher)

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][].

```dart
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;

late bool yell;

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

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

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

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.

```dart
void main(List 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`:

```yaml
dependencies:
build_cli_annotations: ^1.0.0

dev_dependencies:
build_cli: ^1.0.0
build_runner: ^1.0.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](https://pub.dev/packages/args) under the covers.

# More examples:

- The package contains a fully documented
[end-to-end example](https://github.com/kevmoo/build_cli/tree/master/build_cli/example).
- The [test directory](https://github.com/kevmoo/build_cli/tree/master/build_cli/test/src)
contains implementations that exercise most of the features of this package.
- Also look at the
[`package:peanut` source code](https://github.com/kevmoo/peanut.dart).
The `options` files in the
[`src` directory](https://github.com/kevmoo/peanut.dart/tree/master/lib/src)
as the interesting files.

[Dart Build System]: https://github.com/dart-lang/build
[package:build_cli_annotations]: https://pub.dev/packages/build_cli_annotations