Repository: hylang/hy

Issue:
Allow hy2py to use standard input instead of a file
Then, at the command line, you could say

    echo '(print "hello")' | hy2py

instead of

    hy2py <(echo '(print "hello")')

The construct `<(...)` only works in Zsh, anyway.


Discussion:
```
ryan@DevPC-LX ~ $ echo '(print 123 [456] "789 ")' | hy --spy
hy 0.11.0 using CPython(default) 2.7.6 on Linux
=> print(123L, [456L], u'789 ')
123 [456L] 789 

=> ryan@DevPC-LX ~ $ 
```
True, but that executes the code, which one doesn't always want to do.
>The construct `<(...)` only works in Zsh, anyway.

Bash has heredocs and herestrings
```Bash
# Herestring
hy2py <<<'(print "hello")'
# Heredoc
hy2py <<END
(print "a string" 'a-symbol)
END
```
Adding stdin seems like a fairly easy change but I'm not sure why we need it.
Huh, I didn't realize that Bash here documents worked like files. I expected them to work like strings because that's what they do in Perl.

Anyway, I guess supporting standard input would be nice if only because that's how most Unix filter-like programs work, like the GNU versions of `cat`, `head`, and `expand`.
No, not like files, like streams. `hy2py` would have to accept stdin as you propose for the heredoc examples to work. Your zsh example actually does work in bash, even now.
Oh. Huh.

Resolving patch:
diff --git a/hy/cmdline.py b/hy/cmdline.py
--- a/hy/cmdline.py
+++ b/hy/cmdline.py
@@ -40,7 +40,8 @@
 from hy.lex import LexException, PrematureEndOfInput, tokenize
 from hy.compiler import hy_compile, HyTypeError
 from hy.importer import (ast_compile, import_buffer_to_module,
-                         import_file_to_ast, import_file_to_hst)
+                         import_file_to_ast, import_file_to_hst,
+                         import_buffer_to_ast, import_buffer_to_hst)
 from hy.completer import completion
 from hy.completer import Completer
 
@@ -366,9 +367,12 @@ def hy2py_main():
     import platform
     module_name = "<STDIN>"
 
-    options = dict(prog="hy2py", usage="%(prog)s [options] FILE",
+    options = dict(prog="hy2py", usage="%(prog)s [options] [FILE]",
                    formatter_class=argparse.RawDescriptionHelpFormatter)
     parser = argparse.ArgumentParser(**options)
+    parser.add_argument("FILE", type=str, nargs='?',
+                        help="Input Hy code (use STDIN if \"-\" or "
+                             "not provided)")
     parser.add_argument("--with-source", "-s", action="store_true",
                         help="Show the parsed source structure")
     parser.add_argument("--with-ast", "-a", action="store_true",
@@ -376,16 +380,17 @@ def hy2py_main():
     parser.add_argument("--without-python", "-np", action="store_true",
                         help=("Do not show the Python code generated "
                               "from the AST"))
-    parser.add_argument('args', nargs=argparse.REMAINDER,
-                        help=argparse.SUPPRESS)
 
     options = parser.parse_args(sys.argv[1:])
 
-    if not options.args:
-        parser.exit(1, parser.format_help())
+    stdin_text = None
+    if options.FILE is None or options.FILE == '-':
+        stdin_text = sys.stdin.read()
 
     if options.with_source:
-        hst = pretty_error(import_file_to_hst, options.args[0])
+        hst = (pretty_error(import_file_to_hst, options.FILE)
+               if stdin_text is None
+               else pretty_error(import_buffer_to_hst, stdin_text))
         # need special printing on Windows in case the
         # codepage doesn't support utf-8 characters
         if PY3 and platform.system() == "Windows":
@@ -399,7 +404,9 @@ def hy2py_main():
         print()
         print()
 
-    _ast = pretty_error(import_file_to_ast, options.args[0], module_name)
+    _ast = (pretty_error(import_file_to_ast, options.FILE, module_name)
+            if stdin_text is None
+            else pretty_error(import_buffer_to_ast, stdin_text, module_name))
     if options.with_ast:
         if PY3 and platform.system() == "Windows":
             _print_for_windows(astor.dump(_ast))


Test patch: