Closed saxbophone closed 9 years ago
Whoops, I've just realised this might break if the user wants to specify a variable with a #
in it...
I don't think it's valid to have a variable with a #
in it:
monkey@docker:~$ export FOO=123
monkey@docker:~$ export FOO#BAR=123
-bash: export: `FOO#BAR=123': not a valid identifier
That's true, however it is valid to define a variable which itself contains a #
(in quotes):
$ export FOO='#'
$ echo $FOO
#
I think my code will remove the #
and make turn the first line into:
export FOO='
which would cause an error.
Maybe a space character should be expected before inline comments.
FOO=# #comment
>>> print os.environ['FOO']
'#'
@saxbophone: Good point.
@pedroburon: That seem reasonable.
Yes that solution should work, I will have to modify it to not remove #
comments that are inside quote marks.
Or what if the #
only counts as a comment if it's at the beginning of the line? E.g.:
if line.startswith('#'):
return {}
Though if someone is indenting, the above code would not consider the line below to be a comment:
# an indented comment
so maybe instead:
if line.strip().startswith('#'):
return {}
Yes, that's exactly what I was thinking! Inline comments are a bit trickier as we'll need to make sure that it doesn't remove #
symbols contained in quotes but I think I can sort that.
Okay so what I've done here is changed it to ignore comment symbols that are inside quote marks (for using in variable values). I have also made it ignore blank lines and allow for indented comments. Also see the new tests to confirm this behaviour :smile:
Thanks @pedroburon :smile:
Implements the stripping out of bash-style
# comments
. Lines that start with#
will be ignored while lines that define a variable and also have a comment will still work, everything after the comment is ignored.