unscripted 0.6.2

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 46

unscripted pub package Build Status Coverage Status

Define command-line interfaces using ordinary dart methods and classes.

##Installation

pub global activate den
den install unscripted

##Usage

The following greet.dart script outputs a configurable greeting:

#!/usr/bin/env dart

import 'package:unscripted/unscripted.dart';

main(arguments) => new Script(greet).execute(arguments);

// All metadata annotations are optional.
@Command(help: 'Print a configurable greeting', plugins: const [const Completion()])
@ArgExample('--salutation Hi --enthusiasm 3 Bob', help: 'enthusiastic')
greet(
    @Rest(help: 'Name(s) to greet.')
    List<String> who, {
      @Group.start('Output');
      @Option(help: 'How many !\'s to append.')
      int enthusiasm : 0,
      @Flag(abbr: 'l', help: 'Put names on separate lines.')
      bool lineMode : false,
      @Option(name: 'greeting', help: 'Alternate word to greet with e.g. "Hi".')
      String salutation : 'Hello'
    }) {

  print(salutation +
        who.map((w) => (lineMode ? '\n  ' : ' ') + w).join(',') +
        '!' * enthusiasm);
}

We can call this script as follows:

$ greet.dart Bob
Hello Bob
$ greet.dart --enthusiasm 3 -l --greeting Hi Alice Bob
Hi
  Alice,
  Bob!!!

##Automatic --help

A --help/-h flag is automatically defined:

$ greet.dart --help

Description:

  Print a configurable greeting.

Usage:

  greet.dart [options] [<who>...]

    <who>    Name(s) to greet.

Options:

      --completion         Tab completion for this command.

            [install]      Install completion script to .bashrc/.zshrc.
            [print]        Print completion script to stdout.
            [uninstall]    Uninstall completion script from .bashrc/.zshrc.

  -h, --help               Print this usage information.

  Output:

      --enthusiasm         How many !'s to append.
                           (defaults to "0")

  -l, --line-mode          Put names on separate lines.
      --greeting           Alternate word to greet with e.g. "Hi".
                           (defaults to "Hello")

Examples:

  greet.dart --greeting Hi --enthusiasm 3 Bob # enthusiastic

##Sub-Commands

Sub-commands are represented as SubCommand-annotated instance methods of classes, as seen in the following server.dart:

#!/usr/bin/env dart

import 'dart:io';

import 'package:unscripted/unscripted.dart';
import 'package:path/path.dart' as path;

main(arguments) => new Script(Server).execute(arguments);

class Server {

  final String configPath;

  @Command(
      help: 'Manages a server',
      plugins: const [const Completion()])
  Server({this.configPath: 'config.xml'});

  @SubCommand(help: 'Start the server')
  start({bool clean: false}) {
    print('''
Starting the server.
Config path: $configPath''');
  }

  @SubCommand(help: 'Stop the server')
  stop() {
    print('Stopping the server.');
  }

}

We can call this script as follows:

$ server.dart start --config-path my-config.xml --clean
Starting the server.
Config path: my-config.xml

Help is also available for sub-commands:

$ server.dart help

Available commands:

  start
  help
  stop

Use "server.dart help [command]" for more information about a command.

$ server.dart help stop

Description:

  Stop the server

Usage:

  server.dart stop [options]

Options:

  -h, --help    Print this usage information.

##Parsers

Any value-taking argument (option, positional, rest) can have a "parser" responsible for validating and transforming the string passed on the command line. You can give an argument a parser simply by giving it a type (such as int or DateTime) which has a static parse method, or by specifying the parser named argument of the argument's metadata (Option, Positional, or Rest).

##Plugins

Plugins allow you to mixin reusable chunks of cli-specific functionality (options/flags/commands) on top of your base interface.

To add a plugin to your script, just add an instance of the associated plugin class to the plugins named argument of your @Command annotation. The following plugins are available:

###Tab Completion

Add bash/zsh tab completion to your script:

@Command(/*...*/ plugins: const [const Completion()])

If your script already has sub-commands, this will add a completion sub-command (similar to npm completion), otherwise it adds a --completion option. These can then be used as follows:

# Try the tab-completion without permanently installing.
. <(greet.dart --completion print)
. <(server.dart completion print)

# Install the completion script to .bashrc/.zshrc depending on current shell.
# No-op if already installed.
greet.dart --completion install
server.dart completion install

# Uninstall a previously installed completion script.
# No-op if not installed.
greet.dart --completion uninstall
server.dart completion uninstall

Once installed, the user will be able to tab-complete all aspects of your cli, for example:

Option/Flag names: Say your script is a dart method with a longOptionName named parameter. This becomes --long-option-name in your cli, and once completion is installed, the user can type --l[TAB] and it will be completed to --long-option-name. It will also expand short options to their long equivalents, e.g. -vh[TAB] becomes --verbose --help.

Commands: If your script is a dart class having a @SubCommand() longCommandName method, that becomes a long-command-name sub-command in your cli, and the user can type l[TAB] and it will be completed to long-command-name.

Option/Positional/Rest values: The allowed named parameter of Option, Positional, and Rest specifies the allowed values, and thus completions, for those parameters. For example if you have @Option(allowed: const ['red', 'yellow', 'green']) textColor, and the user types --text-color g[TAB] this will become --text-color green. allowed can also be a callback of one of the following forms:

Iterable<String> complete(String text);
Iterable<String> complete();
Future<Iterable<String>> complete(String text);
Future<Iterable<String>> complete();

where if an arg (e.g. text here) is specified, it represents the last partial word typed by the user when completion is requested, which can be used to filter the results to match that prefix. If the arg is omitted, the filtering is done automatically for you. For example if the option/positional/rest represents a file name, you could emulate the builtin shell file name completion by returning a list of filenames in the current directory.

Tab completion is supported in cygwin, with one minor bug (#64).

###Other Plugins

There are several other plugins planned, and also the ability to write your own is planned, see #62.

##Demo

den uses a large subset of the features above. Run pub global activate den to install, and then den -h to get a feel for the UX provided by unscripted.

0.6.2

  • Add name parameter to Option and Flag ( #102 )
  • Add option groups ( #40 )

0.6.1

  • Allow dynamic help content

0.6.0

  • Deprecated declare in favor of new Script.

Breaking changes:

  • Script.execute now calls the method asynchronously so that it can return a Future for the return value.

0.5.0

Breaking changes:

  • Flags without a null default value now default to null instead of false when neither the flag nor it's negation occur on the command-line (#86)

0.4.6

Features:

  • Improve color-support detection (#73)

0.4.5

Features:

  • Reference scripts as foo instead of foo.dart (expect foo.bat in cygwin) (#80)
  • Add allowTrailingOptions to Command and SubCommand (#70)

0.4.4

Bugfixes:

  • Fixed omission of {Rest,Positional}.allowed (#79)

0.4.3

Features:

  • Colorful help output (#68)
  • Add help output for positional arguments (#42)
  • Add metaVar configuration (#43)
  • Only fail completion in windows on actual usage not definition (#75)
  • Support hidden commands (#69)
  • Allow omitting arg to allowed callback (#67)
  • Allow latest args version (#74)

Bugfixes:

  • Fix some checked mode errors
  • Fix running scripts via pub run

0.4.0

Features:

  • Tab Completion! (#7)
  • Initial plugin support (see #62)
  • Improved examples to demo parsers and other metadata

Breaking changes:

  • Removed CallStyle

0.3.2

Bugfixes:

  • dart:mirror closurization operator removed (#50)

0.3.0

Features:

  • Demo: complete *nix cat implementation (#28)
  • Derive parsers from type annotations (#31)
  • Derive rest parameter from List type annotation (#36)
  • Derive allowMultiple from List type annotation (#34)
  • Add ellipsis to rest parameter help formatting (#22)
  • Include script name in error messages (#32)
  • Input and output parsers which transparently handle both file paths and '-' for stdin/stdout (#23)

Breaking changes:

  • Renamed sketch to declare (#37)
  • Rest.min changed to Rest.required (#26)
  • Flag.negatable now defaults to false (#25)

0.2.0

Features:

  • Support argument parsers (#5)
  • Support hierarchical sub-commands (#15)

Breaking changes:

  • @Command metadata must now be placed on the unnamed constructor instead of the class.
  • allowedHelp in Option is now merged into allowed.

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  unscripted: "^0.6.2"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:unscripted/unscripted.dart';
        
Version Uploaded Documentation Archive
0.6.2 Jun 17, 2015 Go to the documentation of unscripted 0.6.2 Download unscripted 0.6.2 archive
0.6.1+1 Apr 29, 2015 Go to the documentation of unscripted 0.6.1+1 Download unscripted 0.6.1+1 archive
0.6.1 Apr 24, 2015 Go to the documentation of unscripted 0.6.1 Download unscripted 0.6.1 archive
0.6.0 Mar 20, 2015 Go to the documentation of unscripted 0.6.0 Download unscripted 0.6.0 archive
0.5.2+1 Dec 22, 2014 Go to the documentation of unscripted 0.5.2+1 Download unscripted 0.5.2+1 archive
0.5.2 Dec 22, 2014 Go to the documentation of unscripted 0.5.2 Download unscripted 0.5.2 archive
0.5.1 Dec 10, 2014 Go to the documentation of unscripted 0.5.1 Download unscripted 0.5.1 archive
0.5.0 Dec 10, 2014 Go to the documentation of unscripted 0.5.0 Download unscripted 0.5.0 archive
0.4.7 Oct 21, 2014 Go to the documentation of unscripted 0.4.7 Download unscripted 0.4.7 archive
0.4.6 Oct 17, 2014 Go to the documentation of unscripted 0.4.6 Download unscripted 0.4.6 archive

All 29 versions...

Analysis

This feature is new.
We welcome feedback.

We analyzed this package, and provided a score, details, and suggestions below.

  • tool failures on Dec 6, 2017
  • Dart: 2.0.0-dev.8.0
  • pana: 0.7.3+1

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
67
Health:
Code health derived from static analysis. [more]
41
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
0
Overall score:
Weighted score of the above. [more]
46

Platforms

Detected platforms:

Error(s) prevent platform classification.

Suggestions

  • Fix lib/src/invocation_maker.dart.

    Strong-mode analysis of lib/src/invocation_maker.dart failed with the following error:

    line: 37 col: 3
    Invalid override. The type of '_GetterInvocationMaker._passThrough' ('(InstanceMirror) → dynamic') isn't a subtype of 'InvocationMaker._passThrough' ('(dynamic) → dynamic').

  • Fix lib/src/plugins/help/option_help.dart.

    Strong-mode analysis of lib/src/plugins/help/option_help.dart failed with the following error:

    line: 53 col: 9
    The argument type 'AnsiPen' can't be assigned to the parameter type '(String) → String'.

  • Fix further 2 Dart files.

    Similar analysis of the following files failed:

    • lib/src/usage.dart
    • lib/src/util.dart

Dependencies

Package Constraint Resolved Available
Direct dependencies
ansicolor >=0.0.9 <0.1.0 0.0.9
args >=0.11.0 <0.14.0 0.13.7 1.2.0
collection >=1.1.0 <2.0.0 1.14.3
mockable_filesystem >=0.0.3 <0.1.0 0.0.3
path >=1.3.3 <2.0.0 1.5.1
supports_color >=0.1.1 <0.2.0 0.1.1
Transitive dependencies
logging 0.11.3+1
Dev dependencies
grinder >=0.7.1+3
test >=0.12.1 <0.12.2