Create a Stata program that functions as a prefix

The following code allows you to create a Stata program which functions as a prefix to other commands, separated by a colon. For example:

fb_graphsave, suffix(png): twoway line y x

Code:

cap program drop fb_graphsave
program define fb_graphsave
  
  * Split the overall command, stored in `0' in a left and right part.
  gettoken left right2 : 0, parse(":")
  if `"`left'"' == ":" {
    local right `"`right2'"'
  }
  else {
   gettoken right3 right : right2, parse(":")
  }

  * Now, do the regular syntax parsing
  local 0 : copy local left
  syntax, suffix(str asis) [noOUTPUT width(real 0.45) noNOTE *]
  
  * Run any code you want to run before the command on the right
  * Now, run the command on the right
  `right'

  * Run any code you want to run after the command on the right

end