2015-03-07 20:50:43 -06:00
|
|
|
==========================
|
|
|
|
Frequently Asked Questions
|
|
|
|
==========================
|
|
|
|
Ok, so, maybe no one actually asked them.
|
|
|
|
|
|
|
|
1. Why xonsh?
|
|
|
|
-------------
|
2015-07-25 01:46:15 -06:00
|
|
|
The idea for xonsh first struck while I was reviewing the BASH chapter
|
2015-03-07 22:41:47 -06:00
|
|
|
(written by my co-author `Katy Huff <http://katyhuff.github.io/>`_)
|
|
|
|
of `Effective Computation in Physics <http://physics.codes/>`_. In the book
|
2015-07-25 01:46:15 -06:00
|
|
|
we spend a bunch of time describing important, but complex ideas, such
|
2015-03-07 22:41:47 -06:00
|
|
|
as piping. However, we don't even touch on more 'basic' aspects of the BASH
|
|
|
|
language, such as if-statements or loops. Even though I have been using BASH
|
|
|
|
for well over a decade, I am not even sure I *know how*
|
|
|
|
to add two numbers together in it or consistently create an array. This is
|
|
|
|
normal.
|
|
|
|
|
|
|
|
If the tool is so bad, then maybe we need a new tool. So xonsh is really meant
|
2015-07-25 01:46:15 -06:00
|
|
|
to solve the problem that other shells don't "fit your brain."
|
|
|
|
In some programing situations this is OK because of what you get
|
2015-03-07 22:41:47 -06:00
|
|
|
(an optimizing compiler, type safety, provable correctness, register access).
|
|
|
|
But a shell that doesn't fit your brain is only a liability.
|
|
|
|
|
2015-07-25 01:46:15 -06:00
|
|
|
Coincidentally, within the week, `an article floated to the top of Hacker News <http://stephen-brennan.com/2015/01/16/write-a-shell-in-c/>`_
|
|
|
|
that teaches you how to write a shell in C. So I thought, "It can't be
|
2015-03-07 22:41:47 -06:00
|
|
|
that hard..."
|
|
|
|
|
|
|
|
And thus, `again <http://exofrills.org>`_, I entered the danger zone.
|
|
|
|
|
2015-03-07 20:50:43 -06:00
|
|
|
|
|
|
|
2. Why not another exotic shell, such as ``fish``?
|
|
|
|
-----------------------------------------------------
|
|
|
|
While many other alternative shells have an amazing suite of features
|
2015-07-25 01:46:15 -06:00
|
|
|
as well as much improved syntax of traditional options, none of them
|
|
|
|
are quite as beautiful as Python. In xonsh, you get the best of all possible
|
|
|
|
worlds. A syntax that already fits your brain and any features that you
|
2015-03-07 20:50:43 -06:00
|
|
|
desire.
|
|
|
|
|
2015-03-07 22:41:47 -06:00
|
|
|
|
2015-03-07 20:50:43 -06:00
|
|
|
3. Why not just use the IPython command line interface?
|
|
|
|
-------------------------------------------------------
|
2015-07-25 01:46:15 -06:00
|
|
|
There are two serious drawbacks to this approach - though believe me I have
|
|
|
|
tried it.
|
2015-03-07 20:50:43 -06:00
|
|
|
|
2015-07-25 01:46:15 -06:00
|
|
|
The first is that typing ``!`` before every subprocess command is
|
|
|
|
extremely tedious. I think that this is because it is a prefix operator and
|
|
|
|
thus gets in the way of what you are trying to do right as you start to try
|
|
|
|
to do it. Making ``!`` a postfix operator could address some of this, but
|
2015-03-07 20:50:43 -06:00
|
|
|
would probably end up being annoying, though not nearly as jarring.
|
|
|
|
|
|
|
|
The second reason is that tab completion of subprocess commands after an ``!``
|
2015-07-25 01:46:15 -06:00
|
|
|
does not work. This is a deal breaker for day-to-day use.
|
2015-03-08 00:32:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
4. So how does this all work?
|
|
|
|
-----------------------------
|
2015-07-25 01:46:15 -06:00
|
|
|
We use `PLY <http://www.dabeaz.com/ply/ply.html>`_ to tokenize and parse
|
2015-03-08 00:32:17 -06:00
|
|
|
xonsh code. This is heavily inspired by how `pycparser <https://github.com/eliben/pycparser>`_
|
|
|
|
used this PLY. From our parser, we construct an abstract syntax tree (AST)
|
2015-07-25 01:46:15 -06:00
|
|
|
only using nodes found in the Python ``ast`` standard library module.
|
2015-03-08 00:32:17 -06:00
|
|
|
This allows us to compile and execute the AST using the normal Python tools.
|
|
|
|
|
2015-07-25 01:46:15 -06:00
|
|
|
Of course, xonsh has special builtins, so the proper context
|
|
|
|
(builtins, globals, and locals) must be set up prior to actually executing
|
|
|
|
any code. However, the AST can be constructed completely independently of
|
|
|
|
any context...mostly.
|
|
|
|
|
|
|
|
While the grammar of the xonsh language is context-free, it was convenient
|
|
|
|
to write the executer in a way that is slightly context sensitive. This is
|
|
|
|
because certain expressions are ambiguous as to whether they belong to
|
|
|
|
Python-mode or subprocess-mode. For example, most people will look at
|
|
|
|
``ls -l`` and see a listing command. However, if ``ls`` and ``l`` were
|
|
|
|
Python variables, this could be transformed to the equivalent (Python)
|
|
|
|
expressions ``ls - l`` or ``ls-l``. Neither of which are valid listing
|
2015-03-08 00:32:17 -06:00
|
|
|
commands.
|
|
|
|
|
2015-07-25 01:46:15 -06:00
|
|
|
What xonsh does to overcome such ambiguity is to check if the left-most
|
2015-03-08 00:32:17 -06:00
|
|
|
name (``ls`` above) is in the present Python context. If it is, then it takes
|
|
|
|
the line to be valid xonsh as written. If the left-most name cannot be found,
|
2015-07-25 01:46:15 -06:00
|
|
|
then xonsh assumes that the left-most name is an external command. It thus
|
|
|
|
attempts to parse the line after wrapping it in an uncaptured subprocess
|
|
|
|
call ``$[]``. If wrapped version successfully parses, the ``$[]`` version
|
2015-03-08 00:32:17 -06:00
|
|
|
stays. Otherwise the original line is retained.
|
|
|
|
|
2015-07-25 01:46:15 -06:00
|
|
|
All of the context sensitive parsing occurs as an AST transformation prior to
|
2015-03-08 00:32:17 -06:00
|
|
|
any code is executed. This ensures that code will never be partially executed
|
|
|
|
before failing.
|
|
|
|
|
2015-07-25 01:46:15 -06:00
|
|
|
It is critical to note that the context sensitive parsing is a convenience
|
|
|
|
meant for humans. If ambiguity remains or exactness is required, simply
|
2015-03-08 00:32:17 -06:00
|
|
|
manually use the ``$[]`` or ``$()`` operators on your code.
|
|
|
|
|
|
|
|
|
|
|
|
5. Context-sensitive parsing is gross
|
|
|
|
--------------------------------------
|
|
|
|
Yes, context-sensitive parsing is gross. But the point of xonsh is that it
|
|
|
|
is ultimately a lot less gross than other shell languages, such as BASH.
|
2015-03-08 01:01:59 -06:00
|
|
|
Furthermore, its use is heavily limited here.
|