Skip to content

Commit

Permalink
parse positional arguments before argparse
Browse files Browse the repository at this point in the history
Side effect: 'files' is now optional (Fixes #16)
  • Loading branch information
peritus committed Dec 13, 2013
1 parent d9ee20e commit 9bea55d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
69 changes: 47 additions & 22 deletions bumpversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,43 @@ def bump(self, part_name):
elif bumped:
self._parsed[label].zero()

OPTIONAL_ARGUMENTS_THAT_TAKE_VALUES = [
'--config-file',
'--current-version',
'--message',
'--new-version',
'--parse',
'--serialize',
'--tag-name',
]


def split_args_in_optional_and_positional(args):
# manually parsing positional arguments because stupid argparse can't mix
# positional and optional arguments

positions = []
for i, arg in enumerate(args):

previous = None

if i > 0:
previous = args[i-1]

if ((not arg.startswith('--')) and
(previous not in OPTIONAL_ARGUMENTS_THAT_TAKE_VALUES)):
positions.append(i)

positionals = [arg for i, arg in enumerate(args) if i in positions]
args = [arg for i, arg in enumerate(args) if i not in positions]

return (positionals, args)

def main(original_args=None):

positionals, args = split_args_in_optional_and_positional(original_args)


def main(args=None):

parser1 = argparse.ArgumentParser(add_help=False)

Expand Down Expand Up @@ -275,19 +310,7 @@ def main(args=None):
help='How to format what is parsed back to a version',
default='{major}.{minor}.{patch}')

parser2_2 = argparse.ArgumentParser(
description=DESCRIPTION,
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
conflict_handler='resolve',
add_help=False,
parents=[parser2],
)
parser2_2.add_argument('part', help='Part of the version to be bumped.', nargs='?')

known_args, remaining_argv = parser2_2.parse_known_args(remaining_argv)

if known_args.part:
remaining_argv[0:0] = [known_args.part]
known_args, remaining_argv = parser2.parse_known_args(args)

defaults.update(vars(known_args))

Expand All @@ -304,7 +327,10 @@ def main(args=None):

if not 'new_version' in defaults and known_args.current_version:
v.parse(known_args.current_version)
v.bump(known_args.part)

if len(positionals) > 0:
v.bump(positionals[0])

defaults['new_version'] = v.serialize()

parser3 = argparse.ArgumentParser(
Expand Down Expand Up @@ -355,29 +381,28 @@ def main(args=None):
parser3.add_argument('part',
help='Part of the version to be bumped.')
parser3.add_argument('files', metavar='file',
nargs='+' if len(files) == 0 else '*',
nargs='*',
help='Files to change', default=files)

args = parser3.parse_args(remaining_argv)
args = parser3.parse_args(remaining_argv + positionals)

if len(args.files) is 0:
warnings.warn("No files specified")
files = files or positionals[1:]

for vcs in VCS:
if vcs.is_usable():
vcs.assert_nondirty()
break

# make sure files exist and contain version string
for path in args.files:
for path in files:
with io.open(path, 'rb') as f:
before = f.read().decode('utf-8')

assert args.current_version in before, 'Did not find string {} in file {}'.format(
args.current_version, path)

# change version string in files
for path in args.files:
for path in files:
with io.open(path, 'rb') as f:
before = f.read().decode('utf-8')

Expand All @@ -390,7 +415,7 @@ def main(args=None):
with io.open(path, 'wt', encoding='utf-8') as f:
f.write(after)

commit_files = args.files
commit_files = files

if config:
config.remove_option('bumpversion', 'new_version')
Expand Down
4 changes: 2 additions & 2 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
[--current-version VERSION] [--dry-run] --new-version VERSION
[--commit | --no-commit] [--tag | --no-tag]
[--tag-name TAG_NAME] [--message COMMIT_MSG]
part file [file ...]
part [file [file ...]]
%s
positional arguments:
part Part of the version to be bumped.
file Files to change
file Files to change (default: [])
optional arguments:
-h, --help show this help message and exit
Expand Down

0 comments on commit 9bea55d

Please sign in to comment.