I am trying to write a snippet that allows me to write a helpful usage string for my Python script, and then automatically insert a "if len(argv) != ..." thing below that:
""" $TM_FILENAME ${1:args} """
if len(argv) != <some regex that counts number of words in $1>: exit(1)
$0
Depending on the number of words that I enter for args, I want the appropriate number to be inserted in front of "if len(argv) != ...". What regular expression can do that?
If a regex can't do that, can I somehow insert the results of a shell script (e.g. wc) in a regular expression (which, by the way, seems like a useful thing to be able to do anyways!).
Thanks! Abhi
On 05.12.2008, at 07:07, Abhimanyu Lad wrote:
""" $TM_FILENAME ${1:args} """ if len(argv) != <some regex that counts number of
words in $1>: exit(1)
$0
Depending on the number of words that I enter for args, I want the appropriate number to be inserted in front of "if len(argv) != ...".
Ad hoc I have no solution by using a single regexp but a quick&dirty solution would be this snippet:
""" $TM_FILENAME ${1:args} """
if len(argv) != ${2:ruby -e 'p "$1".strip.split(/\S+/).size'}: exit(1) $0
invoke it, type something for args, press TAB, press CTRL+OPT+R (it executes the selection in bash and replaces the selection by the result)
Or write a tmcommand which counts the words in selection and replaces the selection with that number. then: """ $TM_FILENAME ${1:args} """
if len(argv) != ${2:$1}: exit(1) $0
type args, press TAB, invoke that tmcommand
--Hans