diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..b3d20e8 --- /dev/null +++ b/Package.resolved @@ -0,0 +1,16 @@ +{ + "object": { + "pins": [ + { + "package": "swift-argument-parser", + "repositoryURL": "https://github.com/apple/swift-argument-parser.git", + "state": { + "branch": null, + "revision": "6b2aa2748a7881eebb9f84fb10c01293e15b52ca", + "version": "0.5.0" + } + } + ] + }, + "version": 1 +} diff --git a/Package.swift b/Package.swift index 566efe5..36fad8d 100644 --- a/Package.swift +++ b/Package.swift @@ -5,16 +5,23 @@ import PackageDescription let package = Package( name: "PassGen", + platforms: [ + .macOS(.v12) + ], dependencies: [ // Dependencies declare other packages that this package depends on. // .package(url: /* package url */, from: "1.0.0"), + + .package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.4.0"), ], targets: [ // Targets are the basic building blocks of a package. A target can define a module or a test suite. // Targets can depend on other targets in this package, and on products in packages this package depends on. .executableTarget( name: "PassGen", - dependencies: []), + dependencies: [ + .product(name: "ArgumentParser", package: "swift-argument-parser") + ]), .testTarget( name: "PassGenTests", dependencies: ["PassGen"]), diff --git a/Sources/PassGen/main.swift b/Sources/PassGen/main.swift index 8038172..8719310 100644 --- a/Sources/PassGen/main.swift +++ b/Sources/PassGen/main.swift @@ -1,8 +1,68 @@ -let generator = PasswordGenerator() -let password = generator.generate( - type: .separated(8, 4, .space), - characters: [.numbers, .lowercase, .uppercase] -) +import ArgumentParser +import Foundation -print("Generated password:") -print(password) +struct PassGen: ParsableCommand { + static let configuration = CommandConfiguration( + abstract: "Generates a Random Password", + version: "1.0.0", + subcommands: [Normal.self, Separated.self] + ) +} + +PassGen.main() + +extension PassGen { + struct Normal: ParsableCommand { + static let configuration = CommandConfiguration(abstract: "Generates a string password", version: "1.0.0") + + @Argument(help: "Password length") var length: Int = 16 + + func validate() throws { + guard length >= 4 else { + throw ValidationError("Password length must be at least 4.") + } + } + + mutating func run() throws { + + let generator = PasswordGenerator() + let password = generator.generate( + type: .normal(length), + characters: [.numbers, .lowercase, .uppercase] + ) + + print("Generated password:") + print(password) + } + } + + struct Separated: ParsableCommand { + static let configuration = CommandConfiguration(abstract: "Generates a password with separators", version: "1.0.0") + + @Argument(help: "Word length") var wordLength: Int = 8 + @Argument(help: "Word count") var wordCount: Int = 4 + + func validate() throws { + guard wordLength >= 3 else { + throw ValidationError("Word length must be at least 3.") + } + + guard wordCount >= 2 else { + throw ValidationError("Word count must be at least 2.") + } + } + + mutating func run() throws { + + let generator = PasswordGenerator() + let password = generator.generate( + type: .separated(wordLength, wordCount, .space), + characters: [.numbers, .lowercase, .uppercase] + ) + + print("Generated password:") + print(password) + + } + } +}