2021-12-27 17:54:26 +00:00
|
|
|
|
edit:add-var genstr~ {|@argv|
|
|
|
|
|
use re
|
|
|
|
|
use str
|
2021-11-19 17:37:08 +00:00
|
|
|
|
|
2021-12-27 17:54:26 +00:00
|
|
|
|
fn j {|@strings| str:join '' $strings}
|
|
|
|
|
fn bytes {|begin size| str:from-codepoints (range $begin (+ $begin $size))}
|
|
|
|
|
fn upper { bytes 0x41 26 }
|
|
|
|
|
fn lower { bytes 0x61 26 }
|
|
|
|
|
fn digit { bytes 0x30 10 }
|
|
|
|
|
fn alpha { j (upper) (lower) }
|
|
|
|
|
fn alphanum { j (alpha) (digit) }
|
2021-11-19 17:37:08 +00:00
|
|
|
|
|
2021-12-27 17:54:26 +00:00
|
|
|
|
fn random {|chars|
|
|
|
|
|
var size = (count $chars)
|
|
|
|
|
print $chars[(randint 0 $size)]
|
|
|
|
|
}
|
|
|
|
|
fn is-int {|arg| re:match '^\d+$' $arg}
|
2021-11-19 17:37:08 +00:00
|
|
|
|
|
2021-12-27 17:54:26 +00:00
|
|
|
|
fn help {||
|
|
|
|
|
echo 'Génère une chaîne de caractères aléatoires'
|
|
|
|
|
echo 'Usage: genstr [(-auldh)] [<chars>] [<size>]'
|
|
|
|
|
echo ''
|
|
|
|
|
echo 'Options:'
|
|
|
|
|
echo ' -a Uniquement des lettres ([A-Za-z])'
|
|
|
|
|
echo ' -u Uniquement des lettres majuscules ([A-Z])'
|
|
|
|
|
echo ' -l Uniquement des lettres minuscules ([a-z])'
|
|
|
|
|
echo ' -d Uniquement des chiffres ([0-9])'
|
|
|
|
|
echo ''
|
|
|
|
|
echo '<chars> Liste de caractères possibles'
|
|
|
|
|
echo '<size> Longueur de la chaîne (par défaut: 10)'
|
|
|
|
|
echo ''
|
|
|
|
|
echo 'Si aucune classe de caractères ou liste de caractères n’est fournie, utilise les caractères alphanumériques ([A-Za-z0-9]).'
|
|
|
|
|
}
|
2021-11-19 17:37:08 +00:00
|
|
|
|
|
|
|
|
|
var len chars h = 0 '' $false
|
2021-12-11 21:09:02 +00:00
|
|
|
|
each {|e|
|
2021-11-19 17:37:08 +00:00
|
|
|
|
if (or (eq $e -h) (eq $e --help)) {
|
|
|
|
|
set h = $true
|
2021-12-27 17:54:26 +00:00
|
|
|
|
help
|
2021-11-19 17:37:08 +00:00
|
|
|
|
break
|
|
|
|
|
} elif (eq $e -a) {
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set chars = (j $chars (alpha))
|
2021-11-19 17:37:08 +00:00
|
|
|
|
} elif (eq $e -u) {
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set chars = (j $chars (upper))
|
2021-11-19 17:37:08 +00:00
|
|
|
|
} elif (eq $e -l) {
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set chars = (j $chars (lower))
|
2021-11-19 17:37:08 +00:00
|
|
|
|
} elif (eq $e -d) {
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set chars = (j $chars (digit))
|
|
|
|
|
} elif (and (is-int $e) (> $e 0)) {
|
2021-11-19 17:37:08 +00:00
|
|
|
|
set len = $e
|
|
|
|
|
} else {
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set chars = (j $chars $e)
|
2021-11-19 17:37:08 +00:00
|
|
|
|
}
|
2021-12-27 17:54:26 +00:00
|
|
|
|
} $argv
|
2021-11-19 17:37:08 +00:00
|
|
|
|
|
|
|
|
|
if (not $h) {
|
|
|
|
|
if (== $len 0) {
|
|
|
|
|
set len = 10
|
|
|
|
|
}
|
|
|
|
|
if (eq $chars '') {
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set chars = (alphanum)
|
2021-11-19 17:37:08 +00:00
|
|
|
|
}
|
2021-12-27 17:54:26 +00:00
|
|
|
|
set @chars = (str:split '' $chars)
|
|
|
|
|
range $len | each {|_| random $chars}
|
2021-11-19 17:37:08 +00:00
|
|
|
|
echo
|
|
|
|
|
}
|
|
|
|
|
}
|