Remove code duplication for command arguments.

This commit is contained in:
Olivier Mangin 2018-08-09 19:19:29 +02:00
parent a8de97c327
commit 8eef7bd77b
No known key found for this signature in database
GPG Key ID: D72FEC1C3120A884
3 changed files with 43 additions and 29 deletions

20
pubs/command_utils.py Normal file
View File

@ -0,0 +1,20 @@
"""Contains code that is reused over commands, like argument definition
or help messages.
"""
def add_doc_add_arguments(parser, move=True):
doc_add_group = parser.add_mutually_exclusive_group()
doc_add_group.add_argument(
'-L', '--link', action='store_const', dest='doc_add', const='link',
default=None,
help="don't copy document files, just create a link.")
doc_add_group.add_argument(
'-C', '--copy', action='store_const', dest='doc_add', const='copy',
default=None,
help="copy document (keep source).")
if move:
doc_add_group.add_argument(
'-M', '--move', action='store_const', dest='doc_add', const='move',
default=None,
help="move document (copy and remove source).")

View File

@ -12,6 +12,7 @@ from .. import apis
from .. import pretty from .. import pretty
from .. import utils from .. import utils
from .. import endecoder from .. import endecoder
from ..command_utils import add_doc_add_arguments
from ..completion import CommaSeparatedTagsCompletion from ..completion import CommaSeparatedTagsCompletion
@ -34,19 +35,7 @@ def parser(subparsers, conf):
).completer = CommaSeparatedTagsCompletion(conf) ).completer = CommaSeparatedTagsCompletion(conf)
parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.', parser.add_argument('-k', '--citekey', help='citekey associated with the paper;\nif not provided, one will be generated automatically.',
default=None, type=p3.u_maybe) default=None, type=p3.u_maybe)
doc_add_group = parser.add_mutually_exclusive_group() add_doc_add_arguments(parser)
doc_add_group.add_argument(
'-L', '--link', action='store_const', dest='doc_add', const='link',
default=None,
help="don't copy document files, just create a link.")
doc_add_group.add_argument(
'-M', '--move', action='store_const', dest='doc_add', const='move',
default=None,
help="move document instead of of copying.")
doc_add_group.add_argument(
'-C', '--copy', action='store_const', dest='doc_add', const='copy',
default=None,
help="copy document instead of of move.")
return parser return parser

View File

@ -8,9 +8,9 @@ from .. import endecoder
from .. import bibstruct from .. import bibstruct
from .. import color from .. import color
from ..paper import Paper from ..paper import Paper
from ..uis import get_ui from ..uis import get_ui
from ..content import system_path, read_text_file from ..content import system_path, read_text_file
from ..command_utils import add_doc_add_arguments
_ABORT_USE_IGNORE_MSG = "Aborting import. Use --ignore-malformed to ignore." _ABORT_USE_IGNORE_MSG = "Aborting import. Use --ignore-malformed to ignore."
@ -18,18 +18,22 @@ _IGNORING_MSG = " Ignoring it."
def parser(subparsers, conf): def parser(subparsers, conf):
parser = subparsers.add_parser('import', parser = subparsers.add_parser(
'import',
help='import paper(s) to the repository') help='import paper(s) to the repository')
parser.add_argument('bibpath', parser.add_argument(
'bibpath',
help='path to bibtex, bibtexml or bibyaml file (or directory)') help='path to bibtex, bibtexml or bibyaml file (or directory)')
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True, parser.add_argument(
help="don't copy document files, just create a link.") 'keys', nargs='*',
parser.add_argument('keys', nargs='*',
help="one or several keys to import from the file") help="one or several keys to import from the file")
parser.add_argument('-O', '--overwrite', action='store_true', default=False, parser.add_argument(
'-O', '--overwrite', action='store_true', default=False,
help="Overwrite keys already in the database") help="Overwrite keys already in the database")
parser.add_argument('-i', '--ignore-malformed', action='store_true', default=False, parser.add_argument(
'-i', '--ignore-malformed', action='store_true', default=False,
help="Ignore malformed and unreadable files and entries") help="Ignore malformed and unreadable files and entries")
add_doc_add_arguments(parser, move=False)
return parser return parser
@ -90,9 +94,10 @@ def command(conf, args):
ui = get_ui() ui = get_ui()
bibpath = args.bibpath bibpath = args.bibpath
copy = args.copy doc_add = args.doc_add
if copy is None: if doc_add is None:
copy = conf['main']['doc_add'] in ('copy', 'move') doc_add = conf['main']['doc_add']
copy = doc_add in ('copy', 'move')
rp = repo.Repository(conf) rp = repo.Repository(conf)
# Extract papers from bib # Extract papers from bib