p-hatan/ mirror

pub repository

mirror.p-hatan.ir

unscripted

0.3.0

Declarative command-line interface programming.

Download archive

Readme

unscripted

Build Status

Unscripted is a pub package for declarative command-line interface programming in dart. Command-line interfaces are defined using ordinary method and class declarations, minimally annotated with command-line metadata.
Reflection is used to derive the command-line interface from the declarations.
Command-line arguments are automatically injected into the method or class (constructor). This removes the need for boilerplate logic to define, parse, validate and assign variables for command-line arguments. Since the interface is defined in code, standard refactoring, testing, etc. tools can be used.

##Demo

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

##Usage

(For more detailed usage, see the API docs)

A simple script to output a greeting:

import 'package:unscripted/unscripted.dart';

main(arguments) => declare(greet).execute(arguments);

@Command(help: 'Outputs a greeting')
@ArgExample('--salutation Welcome --exclaim Bob', help: 'enthusiastic')
greet(
    @Rest(help: "Name(s) to greet")
    List<String> who, // A rest parameter, must be last positional.
    {String salutation : 'Hello', // An option, use `@Option(...)` for metadata.
     bool exclaim : false}) { // A flag, use `@Flag(...)` for metadata.

  print('$salutation ${who.join(' ')}${exclaim ? '!' : ''}');

}

(Compare to a traditiional version of this script.)

We can call this script as follows:

$ dart greet.dart Bob
Hello Bob
$ dart greet.dart --salutation Welcome --exclaim Bob
Welcome Bob!

###Automatic --help

Unscripted automatically defines and handles a --help/-h option, allowing for:

$ dart greet.dart --help
Outputs a greeting

Usage:

dart greet.dart [options] WHO...

Options:

-h, --help            Print this usage information.
    --salutation      (defaults to "Hello")
    --[no-]exclaim

Examples:

dart greet.dart --salutation Welcome --exclaim Bob # enthusiastic

###Sub-Commands

Sub-commands are also supported. In this case the script is defined as a class, whose instance methods can be annotated as sub-commands. Assume we have the following 'server.dart':

import 'package:unscripted/unscripted.dart';

main(arguments) => declare(Server).execute(arguments);

class Server {

  final String configPath;

  @Command(help: 'Manages a server')
  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

A 'help' sub-command is also added, which can be used as a synonym for '--help', which outputs all the basic help info plus a list of available commands:

$ dart server.dart help
Available commands:

  start
  help
  stop

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

and as indicated there, sub-command help is also available:

$ dart server.dart help stop
Stop the server

Usage:

dart server.dart stop [options]

Options:

-h, --help    Print this usage information.
Changelog

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
author: Sean Eagan <seaneagan1@gmail.com>
version: 0.3.0
homepage: http://github.com/seaneagan/unscripted
description: Declarative command-line interface programming.
documentation: http://seaneagan.github.com/unscripted
dependencies:
  args: any
  collection: any
  mockable_filesystem: any
  path: any
dev_dependencies:
  quiver: any
  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

args "any"

collection "any"

mockable_filesystem "any"

path "any"