|
| 1 | +using CommandLine; |
| 2 | +using Common.BasicHelper.Utils; |
| 3 | + |
| 4 | +namespace Common.BasicHelper.Cli.Arguments.Verbs; |
| 5 | + |
| 6 | +[Verb("password", aliases: ["passwd", "pwd"], HelpText = "Password related utils.")] |
| 7 | +public class VerbPassword : BasicOptions |
| 8 | +{ |
| 9 | + [Option('g', "generate", Default = true, HelpText = "Generate a password.")] |
| 10 | + public bool Generate { get; set; } |
| 11 | + |
| 12 | + [Option('l', "length", Default = 12, HelpText = "Length of the password.")] |
| 13 | + public int Length { get; set; } |
| 14 | + |
| 15 | + [Option('r', "length-range", HelpText = "Length range of the password, like `3,5`.")] |
| 16 | + public string? LengthRange { get; set; } |
| 17 | + |
| 18 | + [Option('u', "ignore-uppercase", HelpText = "Ignore uppercase letters.")] |
| 19 | + public bool IgnoreUppercase { get; set; } |
| 20 | + |
| 21 | + [Option('e', "ignore-lowercase", HelpText = "Ignore lowercase letters.")] |
| 22 | + public bool IgnoreLowercase { get; set; } |
| 23 | + |
| 24 | + [Option('n', "ignore-numbers", HelpText = "Ignore numbers.")] |
| 25 | + public bool IgnoreNumbers { get; set; } |
| 26 | + |
| 27 | + [Option('s', "ignore-symbols", HelpText = "Ignore symbols.")] |
| 28 | + public bool IgnoreSymbols { get; set; } |
| 29 | + |
| 30 | + [Option('U', "supported-uppercase", Default = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", HelpText = "Supported uppercase letters.")] |
| 31 | + public string? SupportedUppercase { get; set; } |
| 32 | + |
| 33 | + [Option('E', "supported-lowercase", Default = "abcdefghijklmnopqrstuvwxyz", HelpText = "Supported lowercase letters.")] |
| 34 | + public string? SupportedLowercase { get; set; } |
| 35 | + |
| 36 | + [Option('N', "supported-numbers", Default = "0123456789", HelpText = "Supported numbers.")] |
| 37 | + public string? SupportedNumbers { get; set; } |
| 38 | + |
| 39 | + [Option('S', "supported-symbols", Default = "!@#$%^&*()_+-=[]{};':,./<>?", HelpText = "Supported symbols.")] |
| 40 | + public string? SupportedSymbols { get; set; } |
| 41 | +} |
| 42 | + |
| 43 | +public static class PasswordExtensions |
| 44 | +{ |
| 45 | + public static VerbPassword Execute(this VerbPassword pwdc) |
| 46 | + { |
| 47 | + int lengthRangeStart = 0; |
| 48 | + int lengthRangeEnd = 0; |
| 49 | + |
| 50 | + var lengthRangeProvided = pwdc.LengthRange is not null; |
| 51 | + |
| 52 | + if (pwdc.LengthRange is not null) |
| 53 | + { |
| 54 | + var splited = pwdc.LengthRange.Split(','); |
| 55 | + |
| 56 | + if (splited.Length != 2) |
| 57 | + throw new ArgumentException("Length range should be like `3,5`."); |
| 58 | + |
| 59 | + lengthRangeProvided = lengthRangeProvided && int.TryParse(splited[0], out lengthRangeStart); |
| 60 | + lengthRangeProvided = lengthRangeProvided && int.TryParse(splited[1], out lengthRangeEnd); |
| 61 | + } |
| 62 | + |
| 63 | + var pwd = Password.GeneratePassword( |
| 64 | + pwdc.Length, |
| 65 | + lengthRangeProvided ? lengthRangeStart : null, |
| 66 | + lengthRangeProvided ? lengthRangeEnd : null, |
| 67 | + pwdc.IgnoreUppercase == false, |
| 68 | + pwdc.IgnoreLowercase == false, |
| 69 | + pwdc.IgnoreNumbers == false, |
| 70 | + pwdc.IgnoreSymbols == false, |
| 71 | + pwdc.SupportedUppercase!, |
| 72 | + pwdc.SupportedLowercase!, |
| 73 | + pwdc.SupportedNumbers!, |
| 74 | + pwdc.SupportedSymbols! |
| 75 | + ); |
| 76 | + |
| 77 | + Console.WriteLine(pwd); |
| 78 | + |
| 79 | + return pwdc; |
| 80 | + } |
| 81 | +} |
0 commit comments