#NEWLINE# def parse_command( commands , view ) : #NEWLINE# #INDENT# ' Inspired by Sublime\'s snippet syntax; "${...}" is a variable.\n    But it\'s slightly different, ${<variable_name>[:[default value][:<Prompt message if not exist>]]}\n    EX) git branch -m ${current_branch} ${new_branch::Enter branch name}\n    ' #NEWLINE# import re #NEWLINE# vars = create_variable_values ( view ) #NEWLINE# asks = [ ] #NEWLINE# templates = [ ] #NEWLINE# if ( not isinstance ( commands , list ) ) : #NEWLINE# #INDENT# commands = [ commands ] #NEWLINE# #UNINDENT# for command in commands : #NEWLINE# #INDENT# parsed = re . split ( '\\${(.*?)}' , command ) #NEWLINE# if ( len ( parsed ) == 1 ) : #NEWLINE# #INDENT# templates . append ( command ) #NEWLINE# continue #NEWLINE# #UNINDENT# template_parts = [ ] #NEWLINE# for ( idx , item ) in enumerate ( parsed , start = 1 ) : #NEWLINE# #INDENT# auto_variable = 0 #NEWLINE# if ( ( idx % 2 ) == 0 ) : #NEWLINE# #INDENT# chs = item . split ( ':' ) #NEWLINE# variable_name = chs [ 0 ] #NEWLINE# if ( not variable_name ) : #NEWLINE# #INDENT# variable_name = ( '_' + str ( auto_variable ) ) #NEWLINE# auto_variable += 1 #NEWLINE# #UNINDENT# v = find_defined_value ( variable_name , vars ) #NEWLINE# if ( v is not None ) : #NEWLINE# #INDENT# template_parts . append ( v ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( len ( chs ) == 2 ) : #NEWLINE# #INDENT# template_parts . append ( chs [ 1 ] ) #NEWLINE# continue #NEWLINE# #UNINDENT# if ( len ( chs ) < 3 ) : #NEWLINE# #INDENT# prompt_message = variable_name #NEWLINE# default_value = '' #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# prompt_message = chs [ 2 ] #NEWLINE# default_value = chs [ 1 ] #NEWLINE# #UNINDENT# asks . append ( dict ( variable = variable_name , message = prompt_message , default = default_value ) ) #NEWLINE# template_parts . append ( ( '{%s}' % variable_name ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# template_parts . append ( item ) #NEWLINE# #UNINDENT# #UNINDENT# templates . append ( '' . join ( template_parts ) ) #NEWLINE# #UNINDENT# return ( asks , templates )