Repeat.swift 950 B

123456789101112131415161718192021222324252627282930313233343536
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Argument Parser open source project
  4. //
  5. // Copyright (c) 2020 Apple Inc. and the Swift project authors
  6. // Licensed under Apache License v2.0 with Runtime Library Exception
  7. //
  8. // See https://swift.org/LICENSE.txt for license information
  9. //
  10. //===----------------------------------------------------------------------===//
  11. import ArgumentParser
  12. @main
  13. struct Repeat: ParsableCommand {
  14. @Option(help: "How many times to repeat 'phrase'.")
  15. var count: Int? = nil
  16. @Flag(help: "Include a counter with each repetition.")
  17. var includeCounter = false
  18. @Argument(help: "The phrase to repeat.")
  19. var phrase: String
  20. mutating func run() throws {
  21. let repeatCount = count ?? 2
  22. for i in 1...repeatCount {
  23. if includeCounter {
  24. print("\(i): \(phrase)")
  25. } else {
  26. print(phrase)
  27. }
  28. }
  29. }
  30. }