-3

I'm currently working on a console app and I would like to parse a line that a user could give to the program.
I tried some code from Split string containing command-line parameters into string[] in C# but it isn't this that I want to do.
These scripts works if I enter something like that -push "test", but the user can enter something like that -push $a 75 $c 5.
I would like a script who, if the user enter this command line:

-p $a 75 $c 5 -p $t "test test" &c 1 -p $c 5 $a 75 $a 76

the parse function would return :

{ { "PUSH", { { "A", 75 }, { "C", 5 } } }, { "PUSH", { { "T", "test test" }, { "C", 1 } } }, { "PUSH", { { "C", 5 }, { "A", 75 }, { "A", 76 } } } }

Then, I would like a modulable script where I could add a command like WAIT and arguments like $ms [int]. (If possible)

Edit :
How to read : identifier[type]
Parser output format :

{ Arg0[Command], Arg0[Command], Arg0[Command]... }

Command format :

{ Name[string], Args[Arg[]] }

Arg format :

{ Name[string], Value[object] }

Exemple output :

{ { "CommandName", { { "Arg0", "arg0" }, { "Arg1", 1 } } } }

T.S.
  • 18,195
  • 11
  • 58
  • 78
Aiixu
  • 29
  • 9
  • It would be helpful if you showed the code you have and describe specifically how it's not working. – Rufus L Nov 28 '18 at 18:46
  • edited :) But I can't show the current code because it does not do this at all. That's why I need help – Aiixu Nov 28 '18 at 18:52
  • You need to show the code that's not working in order for anyone to properly answer (SO is not a code-writing service). Presumably it's similar to the answer of the question you linked? – Rufus L Nov 28 '18 at 18:53
  • To give directions we need to know where you're starting from. – Dour High Arch Nov 28 '18 at 18:55
  • Also, have you looked at this link from the comments of the question you linked? [C#/.NET Command Line Arguments Parser](https://www.codeproject.com/Articles/3111/C-NET-Command-Line-Arguments-Parser) – Rufus L Nov 28 '18 at 18:58
  • I'm starting from a answer from the link, the method with the dll import. – Aiixu Nov 28 '18 at 19:21

1 Answers1

0

In .net you have static void Main(string[] args). So, if you pass something like -push $a 75 $c 5, your args will have 5 items. Lets say you have multiple

-push $a 75 $c 5 -push $b 100 $d 10

You would need to do re-split by -push - cmd = string.Join(" ", args) - you are back to original command line. Now, lets split again

You need some model of course

public class ArgsPoco
{
    public int A {get;set;}
    public int B {get;set;}
    public int C {get;set;}
}

Then you take your command and fill POCO

string[] pushSections = cmd.Split(new[]{"-push"});
foreach (string sect in pushSections )
{
   string[] args = sect.Split('$', StringSplitOptions.RemoveEmptyEntries);
   foreach (string arg in args)
   {
       var poco = new ArgsPoco();
       SetProperty(arg, poco);
   }
   _somePocoList.Add(poco);
}

private void SetProperty(string arg, ArgsPoco poco)
{
    string item = arg.Trim();
    int val = Convert.ToInt32(item.Substring(1).Trim());
    if (item.StartsWith("a"))
        poco.A = val;
    else if (item.StartsWith("b"))
        poco.B = val;
    else if (item.StartsWith("c"))
        poco.C = val;
}

Then, you just decorate your POCO with some JSon serialization attributes from Newtonsoft.Json like

[Json . . . . 
public class ArgsPoco
{
    [Json . . . . 
    public int A {get;set;}
 . . . . 

I m not clear here what specific output you need. But bottom line, you serialize your model the way you want, using existent tools or build custom/

T.S.
  • 18,195
  • 11
  • 58
  • 78