예제 #1
0
        public CommandResult <object> Set(
            CommandEvaluationContext context,
            [Parameter(0, "variable name with or without namespace prefix", false)] string name,
            [Parameter(1, "value that must be assigned to the variable", false)] object value,
            [Parameter(2, "name of the object type to be used in order to convert the provided value (on first assign, will assign a type to the variable. After type is assigned, the value can't be assigned from another type). default type is object", true)] string typeLabel = null,
            [Option("r", "read-only", "for a new variable, set it read only")] bool readOnly = false
            )
        {
            if (!VariableSyntax.HasValidRootNamespace(name))
            {
                name = Variables.Nsp(VariableNamespace.local, name);
            }

            Type type = null;

            if (!string.IsNullOrWhiteSpace(typeLabel))
            {
                type = TypeBuilder.GetType(typeLabel);

                if (type == null)
                {
                    throw new Exception(
                              $"type label not found: '{typeLabel}'. possible values are: {string.Join(",", TypeBuilder.GetTypeLabels().Keys)}{ANSI.CRLF}or any actual .net type fullname case sensitive");
                }
            }

            context.Variables.Set(name, value, readOnly, type);
            context.Variables.Get(name, out var @var, false);

            return(new CommandResult <object>(@var));
        }
예제 #2
0
        /// <summary>
        /// set a typed variable from a string value<br/>
        /// don't set the value if conversion has failed
        /// </summary>
        /// <param name="name">name including namespace</param>
        /// <param name="value">value that must be converted to var type an assigned to the var</param>
        void SetVariable(CommandEvaluationContext context, string name, string value)
        {
            var tn = VariableSyntax.SplitPath(name);
            var t  = new ArraySegment <string>(tn);

            if (context.ShellEnv.Get(t, out var o) && o is DataValue val)
            {
                if (ValueTextParser.ToTypedValue(value, val.ValueType, null, out var v, out _))
                {
                    val.SetValue(v);
                }
            }
            else
            {
                Error($"variable not found: {Variables.Nsp(VariableNamespace.env, context.ShellEnv.Name, name)}", true);
            }
        }
예제 #3
0
        public CommandResult <object> Unset(
            CommandEvaluationContext context,
            [Parameter(0, "variable name with or without namespace prefix", false)] string name
            )
        {
            if (!VariableSyntax.HasValidRootNamespace(name))
            {
                name = Variables.Nsp(VariableNamespace.local, name);
            }

            context.Variables.Get(name, out var @var, true);
            if (@var is IDataObject)
            {
                context.Variables.Unset(name);
            }
            else
            {
                throw new Exception($"can't unset a variable member: '{name}'");
            }

            return(new CommandResult <object>(@var));
        }
예제 #4
0
 private void OutputVariable(VariableSyntax node, string prefix)
 {
     builder.AddFragment(new OutputFragment(prefix, DefaultColour));
     builder.AddFragment(new OutputFragment(node.IdentifierToken.ToString(), VariableColour));
 }
예제 #5
0
 public VariableEvalutor(VariableSyntax syntax)
 {
     Syntax = syntax;
 }