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 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

@ -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(
help='import paper(s) to the repository') 'import',
parser.add_argument('bibpath', help='import paper(s) to the repository')
help='path to bibtex, bibtexml or bibyaml file (or directory)') parser.add_argument(
parser.add_argument('-L', '--link', action='store_false', dest='copy', default=True, 'bibpath',
help="don't copy document files, just create a link.") help='path to bibtex, bibtexml or bibyaml file (or directory)')
parser.add_argument('keys', nargs='*', parser.add_argument(
help="one or several keys to import from the file") 'keys', nargs='*',
parser.add_argument('-O', '--overwrite', action='store_true', default=False, help="one or several keys to import from the file")
help="Overwrite keys already in the database") parser.add_argument(
parser.add_argument('-i', '--ignore-malformed', action='store_true', default=False, '-O', '--overwrite', action='store_true', default=False,
help="Ignore malformed and unreadable files and entries") help="Overwrite keys already in the database")
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 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

Loading…
Cancel
Save