8

How can I test how my Main method handles command-line arguments in Linqpad? I tried

void Main(string[] args)
{
    args.Dump()
}

but it errors

No overload for method 'Main' takes 0 arguments

Edit: I don't want to run Linqpad from the command-line (that would be tedious), I just want to supply args to the main method somehow (so I can quickly test a Main method). Perhaps like this mock-up:

linqpad args

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
  • How are you going to invoke this method (from LinqPad)? – Anders Gustafsson Sep 21 '12 at 08:59
  • There is this implementation request [here](http://linqpad.uservoice.com/forums/18302-linqpad-feature-suggestions/suggestions/307671-command-line-interface), but I think that this feature poses a very high security risk and they don't like to go on this route. – Steve Sep 21 '12 at 09:06
  • 1
    A compile-to-EXE option is in the pipeline, so at that point command-line arguments will make sense. In the meantime, depending on what you're trying to achieve, you might find it useful to call Console.ReadLine (and Util.ReadLine for greater flexibility). – Joe Albahari Sep 21 '12 at 09:11
  • Joe, Anders, I've mocked-up an interface that lets the user supply args to the Main method from inside Linqpad rather than the command-line, ie. add a text input to the toolbar. This would be secure, right? http://i.stack.imgur.com/IJGH1.png – Colonel Panic Sep 21 '12 at 09:21

2 Answers2

11
void Main (string[] args)
{
    #if !CMD
        args = new[] { @"arg1", @"arg2" };
    #endif
    args.Dump();
    ...
Den
  • 146
  • 1
  • 4
  • The CMD variable will be set when called from lprun, but not the GUI. Reference: https://www.linqpad.net/lprun.aspx – kevinpo Jul 11 '22 at 21:26
6

Why don't you do this:

void Main()
{
    var args = new string[] { "arg_one", "arg_two" };
    subMain(args);
}

// Define other methods and classes here
public void subMain(string[] args){
    args.Dump();
}
pim
  • 12,019
  • 6
  • 66
  • 69
Alaeddin Hussein
  • 738
  • 1
  • 7
  • 14