Remove code duplication for command arguments.

main
Olivier Mangin 7 years ago
parent a8de97c327
commit 8eef7bd77b
No known key found for this signature in database
GPG Key ID: D72FEC1C3120A884

@ -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).")

@ -12,6 +12,7 @@ from .. import apis
from .. import pretty
from .. import utils
from .. import endecoder
from ..command_utils import add_doc_add_arguments
from ..completion import CommaSeparatedTagsCompletion
@ -34,19 +35,7 @@ def parser(subparsers, conf):
).completer = CommaSeparatedTagsCompletion(conf)
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)
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(
'-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.")
add_doc_add_arguments(parser)
return parser

@ -8,9 +8,9 @@ from .. import endecoder
from .. import bibstruct
from .. import color
from ..paper import Paper
from ..uis import get_ui
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."
@ -18,18 +18,22 @@ _IGNORING_MSG = " Ignoring it."
def parser(subparsers, conf):
parser = subparsers.add_parser('import',
parser = subparsers.add_parser(
'import',
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)')
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True,
help="don't copy document files, just create a link.")
parser.add_argument('keys', nargs='*',
parser.add_argument(
'keys', nargs='*',
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")
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")
add_doc_add_arguments(parser, move=False)
return parser
@ -90,9 +94,10 @@ def command(conf, args):
ui = get_ui()
bibpath = args.bibpath
copy = args.copy
if copy is None:
copy = conf['main']['doc_add'] in ('copy', 'move')
doc_add = args.doc_add
if doc_add is None:
doc_add = conf['main']['doc_add']
copy = doc_add in ('copy', 'move')
rp = repo.Repository(conf)
# Extract papers from bib

Loading…
Cancel
Save