Bladeren bron

Fix min length in cli

Fixes https://github.com/lesspass/lesspass/issues/408
pull/410/head
Guillaume Vincent 5 jaren geleden
bovenliggende
commit
92aa4f20b2
3 gewijzigde bestanden met toevoegingen van 41 en 4 verwijderingen
  1. +19
    -0
      cli/lesspass/actions.py
  2. +6
    -4
      cli/lesspass/cli.py
  3. +16
    -0
      cli/tests/test_functional.py

+ 19
- 0
cli/lesspass/actions.py Bestand weergeven

@@ -0,0 +1,19 @@
import argparse


class Range(argparse.Action):
def __init__(self, min=None, max=None, *args, **kwargs):
self.min = min
self.max = max
kwargs["metavar"] = "[%d-%d]" % (self.min, self.max)
super(Range, self).__init__(*args, **kwargs)

def __call__(self, parser, namespace, value, option_string=None):
if not (self.min <= value <= self.max):
msg = "invalid choice: %r (choose from %d to %d)" % (
value,
self.min,
self.max,
)
raise argparse.ArgumentError(self, msg)
setattr(namespace, self.dest, value)

+ 6
- 4
cli/lesspass/cli.py Bestand weergeven

@@ -1,6 +1,7 @@
import argparse
import os

from lesspass import actions
from lesspass import version
from lesspass import name
from lesspass import description
@@ -38,9 +39,7 @@ def parse_args(args):
"-v", "--version", action="version", version=version.__version__
)
parser.add_argument(
"site",
nargs="?",
help="site used in the password generation (required)"
"site", nargs="?", help="site used in the password generation (required)"
)
parser.add_argument(
"login", nargs="?", help="login used in the password generation. Default to ''."
@@ -55,8 +54,11 @@ def parse_args(args):
"-L",
"--length",
default=16,
action=actions.Range,
min=5,
max=35,
type=int,
help="password length (default: 16, max: 35)",
help="password length (default: 16, min:5, max: 35)",
)
parser.add_argument(
"-C", "--counter", default=1, type=int, help="password counter (default: 1)"


+ 16
- 0
cli/tests/test_functional.py Bestand weergeven

@@ -0,0 +1,16 @@
import unittest
import pexpect
import signal
import time

import sys


class TestFunctional(unittest.TestCase):
def test_length_below_the_minimum(self):
p = pexpect.spawn(
"python3 lesspass/core.py site login masterpassword --lowercase --digits --length 2"
)
self.assertTrue(
"error: argument -L/--length: invalid choice: 2" in str(p.read())
)

Laden…
Annuleren
Opslaan