7
4

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.

Swift4で書く、引数を取るコマンドラインプログラム

Last updated at Posted at 2018-01-07

Swiftはじめたい...その11 コマンドラインで実行ファイルをつくる。 - Qiita
で紹介されていた、引数を取るコマンドラインプログラムを、2018/01/07現在の状況(swift4.0.3、macOS10.12.6)で動くように修正しました。

コード

take_arguments.swift
import Foundation

func main() -> Int {

    let argv = ProcessInfo.processInfo.arguments

    for i in 0 ..< argv.count {
        print("argv[\(i)] = \(argv[i])")
    }

    return 0
}

//返り値を取らないとwarningが出ます
_ = main()

ターミナル上での実行結果

# コンパイル
$ swiftc -o arg.out take_arguments.swift

# 引数を与えて実行
$ ./arg.out a b c d -1 漢字

# 結果
argv[0] = /Users/user_name/Documents/arg.out
argv[1] = a
argv[2] = b
argv[3] = c
argv[4] = d
argv[5] = -1
argv[6] = 漢字
7
4
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
7
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?