2
2

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のコマンドが完全一致で入力しなくても実行されるので中を見た

Posted at

CrystalのCLIを使う際に、コマンドを最後まで入力していないが、
問題なく実行されていたため中身が気になりました。
例えば

$ crystal docs

$ crystal doc

でも実行できます。

中をみてみます。

Crystalの中身

※該当箇所を抜粋

    if command
      case
      when "init".starts_with?(command)
        options.shift
        init
      when "build".starts_with?(command)
        options.shift
        build
      when "play".starts_with?(command)
        options.shift
        playground
      when "deps".starts_with?(command)
        options.shift
        deps
      when "docs".starts_with?(command)
        options.shift
        docs
      when command == "env"
        options.shift
        env
      when command == "eval"
        options.shift
        eval
      when "run".starts_with?(command)
        options.shift
        run_command(single_file: false)
      when "spec/".starts_with?(command)
        options.shift
        run_specs
      when "tool".starts_with?(command)
        options.shift
        tool
      when "help".starts_with?(command), "--help" == command, "-h" == command
        puts USAGE
        exit
      when "version".starts_with?(command), "--version" == command, "-v" == command
        puts Crystal::Config.description
        exit
      else
        if File.file?(command)
          run_command(single_file: true)
        else
          error "unknown command: #{command}"
        end
      end
    else
      puts USAGE
      exit
    end

String#starts_with? で実現していました。

外部資料

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?