Skip to content

Commit 72f843a

Browse files
committed
Add a command line option for indent width.
1 parent 42d6054 commit 72f843a

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

vars2specs.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
"""Generates argument_specs.yml from variables parsed in role.
66
77
Usage:
8-
vars2specs.py [-c] [-r DIR]
8+
vars2specs.py [-c] [-i IND] [-r DIR]
99
1010
Options:
1111
-c Parse all roles in a collection [default: no]
12+
-i IND --indent IND White space count for yaml indention. [default: 4]
1213
-r DIR --role_dir=DIR Input role directory [default: ./].
1314
"""
15+
from textwrap import indent
1416
import typing
1517
import yaml
1618
import docopt
@@ -71,7 +73,7 @@ class to generate arguments_spec.yml from parsed variables
7173
"""
7274
role_dir: Path
7375
collection: bool
74-
76+
indent: int = 4
7577

7678
def __init__(self, role: str, collection: bool):
7779
self.collection = collection
@@ -119,13 +121,12 @@ def generate_spec(self, path: Path, defined_vars):
119121
except KeyError:
120122
vartype = type(variables[var_name]).__name__ if variables[var_name] is not None else "str"
121123
default = "default: %s" % self.quote_default(variables[var_name], vartype) if variables[var_name] is not None else 'required: true'
122-
results.append("""\
124+
results.append("""%s# line %s of %s
123125
%s:
124-
# line %s of %s
125126
%s
126127
description: "%s"
127128
type: "%s"
128-
""" % (var_name, linenumber, str(rel_path), default, description, vartype))
129+
""" % ((" "*3*self.indent), linenumber, str(rel_path), var_name, default, description, vartype))
129130
return results
130131

131132

@@ -143,7 +144,7 @@ def generate(self):
143144
""" write argument specs """
144145
yaml = YAML()
145146
yaml.preserve_quotes = True
146-
yaml.indent(mapping=4)
147+
yaml.indent(mapping = self.indent)
147148
yaml.width = 800
148149

149150
variable_specs = collections.defaultdict(list)
@@ -175,6 +176,8 @@ def main():
175176
role_dir = args['--role_dir'] or './'
176177
collection = args['-c'] or False
177178
v2s = Vars2Specs(role_dir, collection)
179+
if (args['--indent']):
180+
v2s.indent = int(args['--indent'])
178181
v2s.generate()
179182

180183

0 commit comments

Comments
 (0)