LoginSignup
1
0

More than 5 years have passed since last update.

Swiftでコマンドライン引数を使う

Posted at

C言語やJavaでもおなじみのコマンドライン引数をSwiftでも使うためのメモ

実行環境
OS X El Capitan 10.11.5
Swift 2.2

使い方

Processという列挙型を使います。

// argumentsはStringの配列
Process.arguments.forEach {
    print($0)
}

特にインポートは必要ありません。

出力例

上記のコードをCommandLineArgs.swiftというファイルの中に書き込んで保存します。
terminalから、
swift CommandLineArgs.swift aaa bbb ccc
とコマンドを叩くと、以下のように出力されました。

commandLineArguments.swift
aaa
bbb
ccc

プログラム名そのものがargumentsの最初の要素となっているようです。この辺の仕様はC言語と同じみたいですね。

参考資料

Appleのリファレンスを参考にしました。
https://developer.apple.com/reference/swift/process

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