Skip to content

Commit

Permalink
Add argument parser
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejKolar committed Jun 8, 2022
1 parent 9426345 commit 78d14a3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 8 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
Expand Down
74 changes: 67 additions & 7 deletions Sources/PassGen/main.swift
Original file line number Diff line number Diff line change
@@ -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)

}
}
}

0 comments on commit 78d14a3

Please sign in to comment.