Repository navigation
Expand file tree
/
Copy pathRepeat.swift
More file actions
58 lines (51 loc) · 1.91 KB
/
Copy pathRepeat.swift
File metadata and controls
58 lines (51 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//===----------------------------------------------------------*- swift -*-===//
//
// This source file is part of the Swift Argument Parser open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//
import ArgumentParser
@main
// makes parsers object
struct Repeat: ParsableCommand {
/*
The different Args
> COUNT
I've figured out that if the Int type CAN or WILL be nil, you need to have ?
? means the variable is an OPTIONAL type!
This means it needs to be nil or a number. It cannot contain anything else.
*/
@Option(help: "The number of times to repeat 'phrase'.")
var count: Int? = nil
/*
> COUNTER
This one is an argument to see if there should be a counter included.
Although I do not fully understand right now, changing this to 'true' throws an error.
*/
@Flag(help: "Include a counter with each repetition.")
var includeCounter = false
@Argument(help: "The phrase to repeat.")
// TODO: Remove Hello World.
// Added Hello World to see if it would print if there was a phrase
var phrase: String = "Hello, World!"
/*
The run() func is like the main method of the file
It has the process that is needed.
*/
mutating func run() throws {
// Changing the number after '??' will change the amount of times it prints
// Seems as if the ?? is to help with the 'optional' nil type of 'Int?'
let repeatCount = count ?? 2
for i in 1...repeatCount {
if includeCounter {
print("\(i): \(phrase)")
} else {
print(phrase)
} // else
} // for
} // Run function
} // Parsable command