/// <summary> /// Ported from void process_option(const string& whence, const expr_t::func_t& opt, /// </summary> public static void ProcessOption(string whence, ExprFunc opt, Scope scope, string arg, string name) { try { CallScope args = new CallScope(scope); args.PushBack(NValue.Get(whence)); if (!String.IsNullOrEmpty(arg)) { args.PushBack(NValue.Get(arg)); } opt(args); } catch (CountError) { throw; // DM - error_count is not std::exception and may pass by "catch" block } catch (Exception) { if (!String.IsNullOrEmpty(name) && name.StartsWith("-")) { throw new Exception(String.Format("While parsing option '{0}'", name)); } else { throw new Exception(String.Format("While parsing environent variable '{0}'", name)); } } }
public static void ProcessOption(string whence, ExprFunc opt, Scope scope, string arg, string name) { try { CallScope args = new CallScope(scope); args.PushBack(NValue.Get(whence)); if (!String.IsNullOrEmpty(arg)) { args.PushBack(NValue.Get(arg)); } opt(args); } catch { if (!String.IsNullOrEmpty(name) && name.StartsWith("-")) { throw new Exception(String.Format("While parsing option '{0}'", name)); } else { throw new Exception(String.Format("While parsing environent variable '{0}'", name)); } } }
public static CallScope Create(string parm) { CallScope callScope = new CallScope(new EmptyScope()); callScope.PushBack(NValue.StringValue(parm)); return(callScope); }
public void ExecuteCommand(IEnumerable <string> args, bool atRepl) { Session.FlushOnNextDataFile = true; // Process the command verb, arguments and options if (atRepl) { args = ReadCommandArguments(Report, args); if (!args.Any()) { return; } } string verb = args.First(); args = args.Skip(1); // DM - skip the first item that is equal to verb = *arg++; // Look for a precommand first, which is defined as any defined function // whose name starts with "ledger_precmd_". The difference between a // precommand and a regular command is that precommands ignore the journal // data file completely, nor is the user's init file read. // // Here are some examples of pre-commands: // // parse STRING ; show how a value expression is parsed // eval STRING ; simply evaluate a value expression // format STRING ; show how a format string is parsed // // If such a command is found, create the output stream for the result and // then invoke the command. ExprFunc command; bool isPrecommand = false; BindScope boundScope = new BindScope(this, Report); command = LookForPrecommand(boundScope, verb); if (!command.IsNullOrEmpty()) { isPrecommand = true; } // If it is not a pre-command, then parse the user's ledger data at this // time if not done already (i.e., if not at a REPL). Then patch up the // report options based on the command verb. if (!isPrecommand) { if (!atRepl) { Session.ReadJournalFiles(); } Report.NormalizeOptions(verb); command = LookForCommand(boundScope, verb); if (command.IsNullOrEmpty()) { throw new LogicError(String.Format(LogicError.ErrorMessageUnrecognizedCommand, verb)); } } // Create the output stream (it might be a file, the console or a PAGER // subprocess) and invoke the report command. The output stream is closed // by the caller of this function. Report.OutputStream = FileSystem.OutputStreamInitialize( Report.OutputHandler.Handled ? Report.OutputHandler.Str() : String.Empty, Report.PagerHandler.Handled ? Report.PagerHandler.Str() : String.Empty); // Now that the output stream is initialized, report the options that will // participate in this report, if the user specified --options if (OptionsHandler.Handled) { Report.OutputStream.Write(ReportOptions(Report)); } // Create an argument scope containing the report command's arguments, and // then invoke the command. The bound scope causes lookups to happen // first in the global scope, and then in the report scope. CallScope commandArgs = new CallScope(boundScope); foreach (string arg in args) { commandArgs.PushBack(Value.Get(arg)); } var info = Logger.Current.InfoContext(TimerName.Command)?.Message("Finished executing command").Start(); // INFO_START command(commandArgs); info?.Finish(); // INFO_FINISH }