wrapper_args 1.0.1+1

A library for Dart command-line.

Parses raw command-line arguments into a set of options and values. Wrapper package:args/args.dart for my style

Usage #

A simple usage example:

import 'package:wrapper_args/wrapper_args.dart';

main(List<String> arguments) {
  // create root command
  final root = _root();

  // create subcommand
  root.addCommand([
    _server(),
    _client(),
  ]);

  // parse and execute
  root.parseAndExecute(arguments);
}

Command _root() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addFlag(
      "version",
      abbr: "v",
      negatable: false,
      help: "Print the version",
    );
  return Command(
    help: "Usage: wrapper_args [<subcommand>] [<options>]",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print("${command.usage}");
      } else if (results["version"]) {
        print("version 1.0.0");
      }
    },
  );
}

Command _server() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addOption(
      "listen",
      abbr: "l",
      help: "tcp listen address",
    );
  return Command(
    name: "server",
    help: "simulator server",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print(command.usage);
      } else {
        print("server work at ${results['listen']}");
      }
    },
  );
}

Command _client() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addOption(
      "target",
      abbr: "t",
      help: "connect target",
    );
  return Command(
    name: "client",
    help: "simulator client",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print(command.usage);
      } else {
        print("connet ${results['target']} ...");
      }
    },
  );
}

1.0.1 #

  • fix subcommand height > 1 execute height[1]
  • parseAndExecute return execute code

1.0.0 #

  • Initial version, created by Stagehand

example/wrapper_args_example.dart

import 'package:wrapper_args/wrapper_args.dart';
import 'dart:io';

void main(List<String> arguments) {
  // create root command
  final root = _root();

  // create subcommand
  root.addCommand([
    _server(),
    _client(),
  ]);

  // parse and execute
  final code = root.parseAndExecute(arguments);

  // set exit code
  exitCode = code ?? 0;
}

Command _root() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addFlag(
      "version",
      abbr: "v",
      negatable: false,
      help: "Print the version",
    );
  return Command(
    help: "Usage: wrapper_args [<subcommand>] [<options>]",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print("${command.usage}");
      } else if (results["version"]) {
        print("version 1.0.0");
      }
    },
  );
}

Command _server() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addOption(
      "listen",
      abbr: "l",
      help: "tcp listen address",
    );
  return Command(
    name: "server",
    help: "simulator server",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print(command.usage);
      } else {
        String str = results['listen'];
        if (str == null || str.isEmpty) {
          return 1;
        }
        print("server work at $str");
      }
    },
  );
}

Command _client() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addOption(
      "target",
      abbr: "t",
      help: "connect target",
    );
  return Command(
    name: "client",
    help: "simulator client",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print(command.usage);
      } else {
        String str = results['target'];
        if (str == null || str.isEmpty) {
          return 2;
        }
        print("connet ${results['str']} ...");
      }
    },
  )..addCommand(
      [
        _clientHttp(),
      ],
    );
}

Command _clientHttp() {
  final parser = ArgParser()
    ..addFlag(
      "help",
      abbr: "h",
      negatable: false,
      help: "Print this usage information",
    )
    ..addOption(
      "target",
      abbr: "t",
      help: "connect target",
    );

  return Command(
    name: "http",
    help: "simulator http",
    parser: parser,
    execute: (command, results) {
      if (results["help"]) {
        print(command.usage);
      } else {
        String str = results['target'];
        if (str == null || str.isEmpty) {
          return 3;
        }
        print("http connet $str ...");
      }
    },
  );
}

Use this package as a library

1. Depend on it

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


dependencies:
  wrapper_args: ^1.0.1+1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:wrapper_args/wrapper_args.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Aug 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:wrapper_args/wrapper_args.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
args ^1.5.1 1.5.2
Dev dependencies
pedantic ^1.0.0
test ^1.0.0