edo1z blog

プログラミングなどに関するブログです

Dart - Stagehandでコンソールアプリ作ってみる

大体ここに書いてあるやつをやります。

dart.dev

Stagehandを取得

$ pub global activate stagehand

PATHを通す

export PATH="$PATH":"$HOME/.pub-cache/bin"

コンソールアプリ作成

$ stagehand console-full
Creating console-full application `cli`:
  /home/edo/pj/study/dart/cli/.gitignore
  /home/edo/pj/study/dart/cli/CHANGELOG.md
  /home/edo/pj/study/dart/cli/README.md
  /home/edo/pj/study/dart/cli/analysis_options.yaml
  /home/edo/pj/study/dart/cli/bin/main.dart
  /home/edo/pj/study/dart/cli/lib/cli.dart
  /home/edo/pj/study/dart/cli/pubspec.yaml
  /home/edo/pj/study/dart/cli/test/cli_test.dart
8 files written.

--> to provision required packages, run 'pub get'
--> run your app using `dart bin/main.dart`.

ディレクトリ構造

pub get前

$ tree .
.
├── CHANGELOG.md
├── README.md
├── analysis_options.yaml
├── bin
│   └── main.dart
├── lib
│   └── cli.dart
├── pubspec.yaml
└── test
    └── cli_test.dart

3 directories, 7 files

pub get後

$ tree .
.
├── CHANGELOG.md
├── README.md
├── analysis_options.yaml
├── bin
│   └── main.dart
├── lib
│   └── cli.dart
├── pubspec.lock
├── pubspec.yaml
└── test
    └── cli_test.dart

3 directories, 8 files

node_modules的なものはないみたい。pubspec.yamlpackage.json みたいなやつ。

隠しファイルあった

$ tree . -a
.
├── .dart_tool
│   └── pub
│       └── bin
│           ├── sdk-version
│           └── test
│               └── test.dart.snapshot.dart2
├── .gitignore
├── .packages
├── CHANGELOG.md
├── README.md
├── analysis_options.yaml
├── bin
│   └── main.dart
├── lib
│   └── cli.dart
├── pubspec.lock
├── pubspec.yaml
└── test
    └── cli_test.dart

7 directories, 12 files

隠しファイルあった。.packagesというのがnode_modules的なやつっぽい。 実際のコードは、stagehandインストール時にパス設定した、$HOME/.pub-cache/bin の中にいる模様。

.packagesの中身
# Generated by pub on 2019-08-27 21:42:24.417093.
analyzer:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/analyzer-0.38.1/lib/
args:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/args-1.5.2/lib/
async:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/async-2.3.0/lib/
boolean_selector:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/boolean_selector-1.0.5/lib/
charcode:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/charcode-1.1.2/lib/
collection:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/collection-1.14.12/lib/
convert:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/convert-2.1.1/lib/
crypto:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/crypto-2.1.2/lib/
csslib:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/csslib-0.16.1/lib/
front_end:file:///home/edo/.pub-cache/hosted/pub.dartlang.org/front_end-0.1.23/lib/
...

実行

$ dart bin/main.dart 
Hello world: 42!

bin/main.dartの中身

import 'package:cli/cli.dart' as cli;

main(List<String> arguments) {
  print('Hello world: ${cli.calculate()}!');
}

main関数に文字列配列の引数をとっております。 これはコンソールから入力できるようです。

lib/cli.dartの中身

int calculate() {
  return 6 * 7;
}

6 * 7をしております。

コンソールから入力した整数を全部掛け算するようにしてみる

bin/main.dart

import 'package:cli/cli.dart' as cli;

main(List<String> numbers) {
  try {
    print('RESULT: ${cli.calculate(numbers)}');
  } catch (e) {
    print(e);
  }
}

lib/cli.dart

int calculate(List<String> numbers) {
  int result = 1;
  for(String num in numbers) {
    result *= int.parse(num);
  }
  return result;
}

実行結果

$ dart bin/main.dart 12 3 2
RESULT: 72
$ dart bin/main.dart 12 3 2 a b
FormatException: Invalid radix-10 number (at character 1)
a
^