dhylands / rshell

Remote Shell for MicroPython
MIT License
955 stars 137 forks source link

RFC Macro facility #111

Open peterhinch opened 5 years ago

peterhinch commented 5 years ago

This would add a facility to import a Python module containing a dict of macros. At the rshell prompt a macro would be expanded. For example, a user macro copy taking a filename argument would be expanded and run as follows:

> m copy myfile
> cp myfile /flash/my_project_directory/

To include a user specified macro file rshell would be invoked with

$ rshell -m my_macro_file

which would cause it to import my_macro_file.py by searching the Python path in the usual way.

A typical macro file might look like

from my_global_macros import macros  # Optional
macros['copy'] = 'cp {} /flash/my_project/', 'Copy a file to LED flashing project.'

A macro module exports a dict named macros whose key is the macro name. The value is either a string or a 2-tuple. In the latter case element[0] is the expansion and element[1] is a help string. The latter would be accessed by the ml (macro list) command

> ml copy

This would display the expansion string and the help string (if present). Issued with no args, ml would list all available macros.

Note that arguments are supported by Python string format syntax so there can be any number with arbitrary placement in the expansion.

Design

The macro facility is likely to be used by experienced programmers maintaining larger projects. The proposal therefore assumes competence in Python and prioritises flexibility over user friendliness.

There are potential issues over recursive macros and ones whose expansion contains newline characters. I would propose disallowing recursion and whitespace.

Macros would only be expanded at the > prompt. At the $ prompt the user would surely employ scripting. [EDIT] I have prototyped this successfully - I anticipate significant time savings.

Comments welcome, in particular whether anyone else would find this useful.

peterhinch commented 5 years ago

This can be tried via my fork.

dhylands commented 5 years ago

I think that the macro facility is a great idea.

peterhinch commented 5 years ago

In that case I'll submit a PR.

I'm already finding it useful, notably in projects using Python packages with a nested directory structure and long rshell commands.

The solution to the mv command would be safer if there were a fix to https://github.com/dhylands/rshell/issues/112.