Pure Programmer
Blue Matrix


Cluster Map

Command Line Arguments

L1

This page is under construction. Please come back later.

When running programs from the command line we have the option of supplying additional information in the form of arguments on the command line. Command line arguments take the form of strings separated by spaces. For example the following command line program invokation has three additional arguments.

$ swift Program.swift Fred 123 Flintstone

In our programs we can access each of these arguments using the variables Process.arguments[0], Process.arguments[1], Process.arguments[2], etc. The program name can be found in the variable with the 0 subscript. The user supplied arguments are in the variables with subscripts 1, 2, 3, etc.

CmdLineArgs1.swift
#!/usr/bin/env swift;
import Foundation
import Utils

// Begin Main
print("Number of arguments: " + String((CommandLine.arguments.count - 1)))
print("Program Name: " + CommandLine.arguments[0])
print("Arg 1: " + CommandLine.arguments[1])
print("Arg 2: " + CommandLine.arguments[2])
print("Arg 3: " + CommandLine.arguments[3])
print("Arg 4: " + CommandLine.arguments[4])

exit(EXIT_SUCCESS)

Output
$ swiftc CmdLineArgs1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc CmdLineArgs1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)

If we want to treat a command line argument as a number we first need to convert from the string representation passed on the command line to a numeric type. This is where conversion functions come in handy. The example below illustrates how to convert command line arguments to integers.

CmdLineArgs2.swift
#!/usr/bin/env swift;
import Foundation
import Utils

// Begin Main
let a:Int = Int(CommandLine.arguments[1]) ?? 0
let b:Int = Int(CommandLine.arguments[2]) ?? 0
let c:Int = a + b
print(Utils.format("{0:d} + {1:d} = {2:d}", a, b, c))

exit(EXIT_SUCCESS)

Output
$ swiftc CmdLineArgs2.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)

The example below converts the command line arguments to floating point values.

CmdLineArgs3.swift
#!/usr/bin/env swift;
import Foundation
import Utils

// Begin Main
let a:Double = Double(CommandLine.arguments[1]) ?? 0
let b:Double = Double(CommandLine.arguments[2]) ?? 0
let c:Double = a + b
print(Utils.format("{0:f} + {1:f} = {2:f}", a, b, c))

exit(EXIT_SUCCESS)

Output
$ swiftc CmdLineArgs3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)

Questions

Projects

More ★'s indicate higher difficulty level.

References