Skip to content

Commit

Permalink
Setup all options
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrejKolar committed Jun 10, 2022
1 parent 142372d commit 00813fc
Showing 1 changed file with 58 additions and 9 deletions.
67 changes: 58 additions & 9 deletions Sources/PassGen/main.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,69 @@
import ArgumentParser
import Foundation
import ArgumentParser

struct PassGen: ParsableCommand {
static let configuration = CommandConfiguration(
abstract: "Generates a Random Password",
abstract: "Generates a Random Password.",
version: "1.0.0",
subcommands: [Normal.self, Separated.self]
subcommands: [Normal.self, Separated.self],
defaultSubcommand: Normal.self
)
}

PassGen.main()

extension PassGen {
struct Normal: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Generates a string password.", version: "1.0.0")
static let configuration = CommandConfiguration(abstract: "Generates a string password. It uses uppercase, lowercase & number characters by default.", version: "1.0.0")

@Option(name: .shortAndLong, help: "Length of the password.")
var length: Int = 16
var count: Int = 16

@Flag(name: .shortAndLong, help: "Use number characters.")
var numbers = false

@Flag(name: [.long, .customShort("p")], help: "Use uppercase characters.")
var uppercase = false

@Flag(name: [.long, .customShort("w")], help: "Use lowercase characters.")
var lowercase = false

@Flag(name: .shortAndLong, help: "Use symbol characters.")
var symbols = false

func validate() throws {
guard length >= 4 else {
guard count >= 4 else {
throw ValidationError("Password length must be at least 4.")
}
}

mutating func run() throws {
var characters: Set<PasswordGenerator.CharacterType> = []

if numbers {
characters.insert(.numbers)
}

if uppercase {
characters.insert(.uppercase)
}

if lowercase {
characters.insert(.lowercase)
}

if symbols {
characters.insert(.symbols)
}

if characters.count == 0 {
characters = [.lowercase, .uppercase, .numbers]
}

let generator = PasswordGenerator()
let password = generator.generate(
type: .normal(length),
characters: [.numbers, .lowercase, .uppercase]
type: .normal(count),
characters: characters
)

print("Generated password:")
Expand All @@ -38,7 +72,7 @@ extension PassGen {
}

struct Separated: ParsableCommand {
static let configuration = CommandConfiguration(abstract: "Generates a password with separators.", version: "1.0.0")
static let configuration = CommandConfiguration(abstract: "Generates a password with separators. It uses uppercase, lowercase & number characters by default. The default separator is a space.", version: "1.0.0")

@Option(name: .shortAndLong, help: "Length of a single segment.")
var length: Int = 8
Expand All @@ -58,6 +92,15 @@ extension PassGen {
@Flag(name: .shortAndLong, help: "Use bar for separator.")
var bar = false

@Flag(name: .shortAndLong, help: "Use number characters.")
var numbers = false

@Flag(name: [.long, .customShort("p")], help: "Use uppercase characters.")
var uppercase = false

@Flag(name: [.long, .customShort("w")], help: "Use lowercase characters.")
var lowercase = false

func validate() throws {
guard length >= 3 else {
throw ValidationError("Word length must be at least 3.")
Expand All @@ -66,6 +109,12 @@ extension PassGen {
guard count >= 2 else {
throw ValidationError("Word count must be at least 2.")
}

let separatorFlagsOnCount = [dash, slash, underscore, bar].reduce(0) { $0 + ($1 ? 1 : 0) }

if separatorFlagsOnCount > 1 {
throw ValidationError("Pass in only one separator flag.")
}
}

mutating func run() throws {
Expand Down

0 comments on commit 00813fc

Please sign in to comment.