From 972bf9102391eb0368a9359e1c1aaa423e2d5a70 Mon Sep 17 00:00:00 2001 From: k1574 Date: Fri, 6 May 2022 21:26:28 +0500 Subject: [PATCH] pin: implemented generating pins of less length than max. --- pin/pin.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/pin/pin.go b/pin/pin.go index 84ce999..fc4f8fc 100644 --- a/pin/pin.go +++ b/pin/pin.go @@ -5,9 +5,12 @@ import( "fmt" "strconv" "log" + "flag" ) var( + Lflag bool + lval int arg0 string delim rune = '\n' status int = 0 @@ -50,18 +53,37 @@ printPins(s []rune, l int) { func Run(args []string) int { arg0 = args[0] - - if len(args) < 3 { + flagSet := flag.NewFlagSet(args[0], flag.ExitOnError) + flagSet.IntVar(&lval, "l", 0, + "Add combinations with less number of chars starting with arg.") + flagSet.BoolVar(&Lflag, "L", false, + "Set less number to 1. Overrides l flag.") + flagSet.Parse(args[1:]) + args = flagSet.Args() + if len(args) < 2 { usage() } - chrs := []rune(args[1]) - n, err := strconv.Atoi(args[2]) + chrs := []rune(args[0]) + n, err := strconv.Atoi(args[1]) if err!=nil { log.Fatal(err) } - printPins(chrs, n) + if Lflag { + lval = 1 + } + + if lval != 0 { + if lval > n { + usage() + } + for i := lval ; i<=n ; i++ { + printPins(chrs, i) + } + } else { + printPins(chrs, n) + } return status }