mirror of
https://github.com/coursera-dl/coursera-dl.git
synced 2026-07-18 00:45:26 +00:00
Merge pull request #563 from andersonvom/master
Make class_names arg optional for certain flags
This commit is contained in:
commit
ab9f4bfed3
2 changed files with 43 additions and 1 deletions
|
|
@ -14,6 +14,20 @@ from .credentials import get_credentials, CredentialsError, keyring
|
|||
from .utils import decode_input
|
||||
|
||||
|
||||
def class_name_arg_required(args):
|
||||
"""
|
||||
Evaluates whether class_name arg is required.
|
||||
|
||||
@param args: Command-line arguments.
|
||||
@type args: namedtuple
|
||||
"""
|
||||
no_class_name_flags = ['list_courses', 'version']
|
||||
return not any(
|
||||
getattr(args, flag)
|
||||
for flag in no_class_name_flags
|
||||
)
|
||||
|
||||
|
||||
def parse_args(args=None):
|
||||
"""
|
||||
Parse the arguments/options passed to the program on the command line.
|
||||
|
|
@ -27,7 +41,7 @@ def parse_args(args=None):
|
|||
|
||||
group_basic.add_argument('class_names',
|
||||
action='store',
|
||||
nargs='+',
|
||||
nargs='*',
|
||||
help='name(s) of the class(es) (e.g. "ml-005")')
|
||||
|
||||
group_basic.add_argument('-u',
|
||||
|
|
@ -335,6 +349,11 @@ def parse_args(args=None):
|
|||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(message)s')
|
||||
|
||||
if class_name_arg_required(args) and not args.class_names:
|
||||
parser.print_usage()
|
||||
logging.error('You must supply at least one class name')
|
||||
sys.exit(1)
|
||||
|
||||
# show version?
|
||||
if args.version:
|
||||
# we use print (not logging) function because version may be used
|
||||
|
|
|
|||
23
coursera/test/test_commandline.py
Normal file
23
coursera/test/test_commandline.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
"""
|
||||
Test command line module.
|
||||
"""
|
||||
|
||||
from coursera import commandline
|
||||
from coursera.test import test_workflow
|
||||
|
||||
|
||||
def test_class_name_arg_required():
|
||||
args = {'list_courses': False, 'version': False}
|
||||
mock_args = test_workflow.MockedCommandLineArgs(**args)
|
||||
assert commandline.class_name_arg_required(mock_args)
|
||||
|
||||
|
||||
def test_class_name_arg_not_required():
|
||||
not_required_cases = [
|
||||
{'list_courses': True, 'version': False},
|
||||
{'list_courses': False, 'version': True},
|
||||
{'list_courses': True, 'version': True},
|
||||
]
|
||||
for args in not_required_cases:
|
||||
mock_args = test_workflow.MockedCommandLineArgs(**args)
|
||||
assert not commandline.class_name_arg_required(mock_args)
|
||||
Loading…
Add table
Add a link
Reference in a new issue