p-hatan/ mirror

pub repository

mirror.p-hatan.ir

unscripted

0.4.0

Declarative command-line interface programming.

Download archive

Readme

unscripted

Build Status | API docs

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

##Installation

Add the unscripted package to your pubspec.yaml dependencies:

unscripted: >=0.4.0 <0.5.0

##Usage

The following greet.dart script outputs a configurable greeting:

#!/usr/bin/env dart

import 'package:unscripted/unscripted.dart';

main(arguments) => declare(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, {
      @Option(help: 'Alternate word to greet with e.g. "Hi".')
      String salutation : 'Hello',
      @Option(help: 'How many !\'s to append.')
      int enthusiasm : 0,
      @Flag(abbr: 'l', help: 'Put names on separate lines.')
      bool lineMode : false
    }) {

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

We can call this script as follows:

$ greet Bob
Hello Bob
$ greet --salutation Hi --enthusiasm 3 -l Alice Bob
Hi
  Alice,
  Bob!!!

##Automatic --help

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

$ greet.dart --help
Print a configurable greeting

Usage:

  greet.dart [options] [WHO]...

Options:

      --salutation         Alternate word to greet with e.g. "Hi".
      --enthusiasm         How many !'s to append.
  -l, --line-mode          Put names on separate lines.
      --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.

Examples:

  greet.dart --salutation 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) => declare(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}) {
    print('''
Starting the server.
Config path: $configPath''');
  }

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

}

We can call this script as follows:

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

Help is also available for sub-commands:

$ dart server.dart help
Available commands:

  start
  help
  stop

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

$ dart server.dart help stop
Stop the server

Usage:

dart 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:

#!/usr/bin/env dart
// ...
@Command(plugins: const [const Completion()])

(Once pub supports cli's, the "shebang" line will no longer be required.)

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 and Positional 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. In addition to Iterable<String>, allowed can also be a function of the form Iterable<String> complete(String text), or it can even return a Future Future<Iterable<String>> complete(String text). For example if the option/positional 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

cat.dart is a complete implementation of the *nix cat utility using unscripted.

Changelog

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.

Installing

export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add unscripted
pubspec.yaml
name: unscripted
version: 0.4.0
author: Sean Eagan <seaneagan1@gmail.com>
description: Declarative command-line interface programming.
homepage: http://github.com/seaneagan/unscripted
documentation: http://seaneagan.github.com/unscripted
dependencies:
  ansicolor: any
  args: '>=0.11.0 <0.12.0'
  collection: any
  mockable_filesystem: any
  path: any
  quiver: any
dev_dependencies:
  unittest: any

Version history

VersionPublishedArchiveSHA-256
0.0.12013-12-16T19:14:17.630430ZDownload78507f702e08119aef3ba875ee447496b0103d8ad0d4a18b96e9c4a689afb47b
0.0.22013-12-16T22:15:40.684610ZDownloadb113ab4962939e7bb5bb89697ab7939189e0e21717e830ea39a17fd0046fac09
0.1.02013-12-17T17:49:59.672960ZDownload246fcb45dbd60a1322ad304194b95dc5e9bc4c0d506b908e0134215417624cac
0.1.12013-12-17T18:20:04.521040ZDownload0f1888e9fe55ca78142467e6bc240fb1e73d39588bacee102ebd6610648f480b
0.1.22013-12-17T19:29:37.720690ZDownload8002b6609b752b717fdfba620294a6b962daa8defc27c9a8451082c99c0549aa
0.1.32014-01-02T20:10:25.149930ZDownload122e022dcd6ef82c8541a1114b6ecfa05260e802657f52e5c03dd3eeb9655be2
0.1.42014-01-02T21:37:10.387290ZDownload325db271d825fe04b4cfe92ced31bc1725d7fc1636b19672a8908ad8557cd98d
0.2.02014-01-06T22:00:28.168040ZDownload934ea3c29a7b44e93c4cae853cc9de86e51d832656f6160750a8076c060ab11c
0.3.02014-01-15T21:49:36.266240ZDownloade332e69e3bab18a464c7d8b804335ab879922926cf5e90d1d77d07987def2484
0.3.12014-01-17T19:32:15.042310ZDownload39625a7b5a4aa4aa92c3c9f24de101f8f12994d2189fa01481f03f1ad523b8ad
0.3.22014-04-21T15:58:14.715440ZDownload3f2166f299c820899c2df74598471c91a2124c4679894c8d865ebd629c7427e6
0.3.32014-04-22T19:04:35.541520ZDownload38f23967e9bb810af1f0a3cf09548d398256342a51d928246de2bd8a70c50833
0.3.42014-04-24T16:30:30.819800ZDownload2d3cf06722715ba68a15301602aa6d1523dc7db68513b5c368b0d59794602890
0.4.02014-06-11T19:56:01.951210ZDownload7d652f586d61d88c1543fe2e61877b88f7fa8c8b7207c28c4d658c423101f515
0.4.12014-06-12T13:10:29.966490ZDownload9b4e51ab6b76429c3d76e86d7aa53fc4dee3dd55e466bc54b46ad45fa849bfd5
0.4.22014-06-17T21:10:20.430630ZDownloadb559735d5775f5eba520370bcde39ab508cc9edc2933c97370d300e81890eb86
0.4.32014-09-29T16:57:51.991260ZDownload8f1f5f0911717b65c4f9ff57005ff5f55a1d804a4b957363d96bf44a75c0c6df
0.4.42014-10-01T18:37:53.934410ZDownload59bd06a11ef7631bac4aa37fc93537d31333addde349a85ad3a46a3b0c33f26a
0.4.52014-10-09T20:39:24.582240ZDownloadd239a35e72055d9087808225dbf59e0f84113da64652b524486120c786dac165
0.4.62014-10-17T17:41:45.115390ZDownloadaf5f8e2199aab612d8e304ab77331cf58c1b1a2c82817d840c0f3f36ad2807ce
0.4.72014-10-21T13:15:29.619410ZDownload52dbfdfc7354a6ed7219fdd440f8e819f823acc80a8c4ba2d062af296b619669
0.5.02014-12-10T19:27:27.170220ZDownload5aaec75cbfe12071544c6d8bebeb61a8555e4ffd4add7f8e999973ead26764c4
0.5.12014-12-10T22:31:39.625400ZDownload273ed678a2cb94a6229a5e1b8e8d1d3bbb0a465ad74aea44de184f6fb72bdf7d
0.5.22014-12-22T20:11:24.641830ZDownload9622fc6f3f85357de694297bf20f770555365a0918847c810667634f54b0abff
0.5.2+12014-12-22T20:28:19.839580ZDownloadd1cc5d45626fe2cdb442cd5d3a52bf12aa66331bc8fb3ba567d7dc9fe596e4ac
0.6.02015-03-20T20:54:53.497110ZDownload91981ed8a1c72d08e808a820d6a312a78372edc8670f4791527e8b228686b079
0.6.12015-04-24T20:59:18.798920ZDownload6415d425c06be2b3c3f2cd52338c7cb0081dff19274d21f350137fa3897b2e44
0.6.1+12015-04-29T18:35:30.891890ZDownload7d26e02bc3e43ce63e4ec964ec225da6502e43313c8debbf11a73d6f2fc291c3
0.6.22015-06-17T21:11:45.096ZDownloadb5de3f20fafea9d35db0fe3681150878d0dc0d6e5f246d3999159852f4c98d55

Dependencies

ansicolor "any"

args "\u003e=0.11.0 \u003c0.12.0"

collection "any"

mockable_filesystem "any"

path "any"

quiver "any"