Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding --must-exist command line option #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions yamale/command_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ def _validate_dir(root, schema_name, cpus, parser, strict):
raise ValueError('\n----\n'.join(set(error_messages)))


def _router(root, schema_name, cpus, parser, strict=True):
def _router(root, schema_name, cpus, parser, strict=True, must_exist=False):
root = os.path.abspath(root)
if must_exist and not os.path.exists(root):
raise ValueError("Path {} doesn't exist".format(root))
if os.path.isfile(root):
_validate_single(root, schema_name, parser, strict)
else:
Expand All @@ -108,18 +110,20 @@ def _router(root, schema_name, cpus, parser, strict=True):
def main():
parser = argparse.ArgumentParser(description='Validate yaml files.')
parser.add_argument('path', metavar='PATH', default='./', nargs='?',
help='folder to validate. Default is current directory.')
help='Folder to validate. Default is current directory.')
parser.add_argument('-s', '--schema', default='schema.yaml',
help='filename of schema. Default is schema.yaml.')
help='Filename of schema. Default is schema.yaml.')
parser.add_argument('-n', '--cpu-num', default=4, type=int,
help='number of CPUs to use. Default is 4.')
help='Number of CPUs to use. Default is 4.')
parser.add_argument('-p', '--parser', default='pyyaml',
help='YAML library to load files. Choices are "ruamel" or "pyyaml" (default).')
parser.add_argument('--no-strict', action='store_true',
help='Disable strict mode, unexpected elements in the data will be accepted.')
parser.add_argument('--must-exist', action='store_true',
help='Fail if the path does not exist.')
args = parser.parse_args()
try:
_router(args.path, args.schema, args.cpu_num, args.parser, not args.no_strict)
_router(args.path, args.schema, args.cpu_num, args.parser, not args.no_strict, args.must_exist)
except (SyntaxError, NameError, TypeError, ValueError) as e:
print('Validation failed!\n%s' % str(e))
exit(1)
Expand Down