1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Crystalを試す:その2

1
Last updated at Posted at 2016-11-18

前回の記事からの続き

6. 引数を与えてみる

Rubyと同じでARGVに格納されるっぽい
とりあえずhello.crを以下のように変更

hello.cr
puts "Hello world!"
puts ARGV

実行してみる

$ crystal hello.cr
Hello world!
[]

$ crystal hello.cr aaa
Hello world!
["aaa"]

$ crystal hello.cr aaa bbb
Hello world!
["aaa", "bbb"]

$ crystal hello.cr aaa bbb cc,cer
Hello world!
["aaa", "bbb", "cc,cer"]

7.「-h,--help」のようなオプションを使いたい場合

OptionParserクラスというのを使えば良いっぽい
詳細:https://crystal-lang.org/api/0.19.4/OptionParser.html

ということで上記URLのサンプルと同じソースコードにhello.crを変更
puts "hello"puts ARGVは消さずにコメントアウト(なんとなく)

hello.cr
# puts "hello"
# puts ARGV

require "option_parser"

upcase = false
destination = "World"

OptionParser.parse! do |parser|
  parser.banner = "Usage: salute [arguments]"
  parser.on("-u", "--upcase", "Upcases the salute") { upcase = true }
  parser.on("-t NAME", "--to=NAME", "Specifies the name to salute") { |name| destination = name }
  parser.on("-h", "--help", "Show this help") { puts parser }
end

destination = destination.upcase if upcase
puts "Hello #{destination}!"
  • コンパイルせずに実行するときはcrystal hello.cr -- <hello.crに記述したオプション>となる
$ crystal hello.cr
Hello World!

$ crystal hello.cr -- -u
Hello WORLD!

$ crystal hello.cr -- -t "Crystal"
Hello Crystal!

$ crystal hello.cr -- --help
Usage: salute [arguments]
    -u, --upcase                     Upcases the salute
    -t NAME, --to=NAME               Specifies the name to salute
    -h, --help                       Show this help
Hello World!
  • コンパイルした後なら--は不要となる
$ crystal build hello.cr 
$ ./hello -u
Hello WORLD!
$ ./hello --to=Crystal
Hello Crystal!
$ ./hello -t Crystal -u
Hello CRYSTAL!
$ ./hello --help
Usage: salute [arguments]
    -u, --upcase                     Upcases the salute
    -t NAME, --to=NAME               Specifies the name to salute
    -h, --help                       Show this help
Hello World!

8.プロジェクトの作成

crystal init app <project_name>で作成

$ crystal init app Sample
      create  Sample/.gitignore
      create  Sample/LICENSE
      create  Sample/README.md
      create  Sample/.travis.yml
      create  Sample/shard.yml
      create  Sample/src/Sample.cr
      create  Sample/src/Sample/version.cr
      create  Sample/spec/spec_helper.cr
      create  Sample/spec/Sample_spec.cr
Initialized empty Git repository in /usr/local/crystal/Sample/.git/

なんかいっぱい生えた
spec/はたぶんテスト用だろうなー
src/が基本的にソースコードを入れる感じかなー

$ cd /usr/local/crystal/Sample
$ more src/Sample.cr 
require "./Sample/*"

module Sample
  # TODO Put your code here
end
$ more src/Sample/version.cr 
module Sample
  VERSION = "0.1.0"
end

OptionParserで、「-v,--version」指定時にsrc/Sample/version.cr呼べばバージョン表示ができそう
短いけど今回はここまで

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?