Repository: xonsh/xonsh

Issue:
Leading space in "-c" command doesn't work
Leading spaces (or tabs, but not newlines) crashes xonsh.

To reproduce: `xonsh -c ' echo test'`

Traceback:

```
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/xonsh/execer.py", line 139, in _parse_ctx_free
    debug_level=(self.debug_level > 1))
  File "/usr/local/lib/python3.6/site-packages/xonsh/parsers/base.py", line 288, in parse
    tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
  File "/usr/local/lib/python3.6/site-packages/xonsh/ply/yacc.py", line 325, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/usr/local/lib/python3.6/site-packages/xonsh/ply/yacc.py", line 1175, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/usr/local/lib/python3.6/site-packages/xonsh/ply/yacc.py", line 187, in call_errorfunc
    r = errorfunc(token)
  File "/usr/local/lib/python3.6/site-packages/xonsh/parsers/base.py", line 2314, in p_error
    column=p.lexpos))
  File "/usr/local/lib/python3.6/site-packages/xonsh/parsers/base.py", line 383, in _parse_error
    raise err
SyntaxError: 3b12ea2767bb2cf243443f97cddcbbf9:1:0: ('code:  ',)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/xonsh", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/xonsh/main.py", line 233, in main
    run_code_with_cache(args.command, shell.execer, mode='single')
  File "/usr/local/lib/python3.6/site-packages/xonsh/codecache.py", line 180, in run_code_with_cache
    ccode = compile_code(filename, code, execer, glb, loc, mode)
  File "/usr/local/lib/python3.6/site-packages/xonsh/codecache.py", line 96, in compile_code
    ccode = execer.compile(code, glbs=glb, locs=loc, mode=mode)
  File "/usr/local/lib/python3.6/site-packages/xonsh/execer.py", line 96, in compile
    tree = self.parse(input, ctx, mode=mode)
  File "/usr/local/lib/python3.6/site-packages/xonsh/execer.py", line 71, in parse
    tree, input = self._parse_ctx_free(input, mode=mode)
  File "/usr/local/lib/python3.6/site-packages/xonsh/execer.py", line 197, in _parse_ctx_free
    raise original_error
  File "/usr/local/lib/python3.6/site-packages/xonsh/execer.py", line 139, in _parse_ctx_free
    debug_level=(self.debug_level > 1))
  File "/usr/local/lib/python3.6/site-packages/xonsh/parsers/base.py", line 288, in parse
    tree = self.parser.parse(input=s, lexer=self.lexer, debug=debug_level)
  File "/usr/local/lib/python3.6/site-packages/xonsh/ply/yacc.py", line 325, in parse
    return self.parseopt_notrack(input, lexer, debug, tracking, tokenfunc)
  File "/usr/local/lib/python3.6/site-packages/xonsh/ply/yacc.py", line 1175, in parseopt_notrack
    tok = call_errorfunc(self.errorfunc, errtoken, self)
  File "/usr/local/lib/python3.6/site-packages/xonsh/ply/yacc.py", line 187, in call_errorfunc
    r = errorfunc(token)
  File "/usr/local/lib/python3.6/site-packages/xonsh/parsers/base.py", line 2314, in p_error
    column=p.lexpos))
  File "/usr/local/lib/python3.6/site-packages/xonsh/parsers/base.py", line 383, in _parse_error
    raise err
SyntaxError: 3b12ea2767bb2cf243443f97cddcbbf9:1:0: ('code:  ',)
```



Discussion:
Thanks for reporting, @eigilhs!

I think the fix for this would make a reasonable first patch for someone.  In interactive mode, we call `lstrip` on the first line of input we receive from the shell (see [here](https://github.com/scopatz/xonsh/blob/master/xonsh/base_shell.py#L140)).  I expect that we can probably solve this issue by using `lstrip` as well on the input we receive in single-command mode [here](https://github.com/scopatz/xonsh/blob/master/xonsh/main.py#L233).

I'd like to point out that traditional shells will execute a leading space, but Python fails. And so the current behaviour is a side effect our pythonic nature.

``` console
scopatz@localhost ~/xonsh master $ bash -c " echo a"
a
scopatz@localhost ~/xonsh master $ python -c " print('a')"
  File "<string>", line 1
    print('a')
    ^
IndentationError: unexpected indent
```

That said, I think it is reasonable to do an lstrip.


Resolving patch:
diff --git a/xonsh/main.py b/xonsh/main.py
--- a/xonsh/main.py
+++ b/xonsh/main.py
@@ -238,7 +238,7 @@ def main(argv=None):
     shell = builtins.__xonsh_shell__
     if args.mode == XonshMode.single_command:
         # run a single command and exit
-        run_code_with_cache(args.command, shell.execer, mode='single')
+        run_code_with_cache(args.command.lstrip(), shell.execer, mode='single')
     elif args.mode == XonshMode.script_from_file:
         # run a script contained in a file
         if os.path.isfile(args.file):


Test patch: