[Mono-list] csharp REPL

Steve Lessard s_lessard at yahoo.com
Wed Jun 29 12:50:56 EDT 2011


It seems there is an additional obstacle to overcome before I could 
even try to get command line arguments from Environment.CommandLine.


$csharp foo bar
cs2001: Source file 
`/source/local/private/main/developer/slessard/tools/foo' not found
$csharp -foo -bar
error CS2007: Unrecognized command-line option: `-foo'


The problem is that csharp.exe complains because it doesn't recognize 
the arguments that are intended for my script. I need a way to pass in 
arguments to my script in a way that won't cause csharp.exe to complain 
about files not found or unrecognized command-line options.  The only 
solution I have been able to find is to prefix each of my script's 
command-line arguments with "-d:" (without the quotes) but that would 
require the script to have additional code to find and strip out the 
"-d:" portion of each argument (see output below.)  Is there any other 
trick that might work? I know very, very little about how the shebang 
works. Would it be possible to leverage the shebang in some crafty way? 
Would it be possible to get the shebang to convert the command line 
arguments into local environment variables named ARG1, ARG2, ..., ARGn?

The is the output I get from my script after adding code to write to 
stout all of the process's command line arguments:

$./timefromseconds -d:foo -d:bar
Environment.GetCommandLineArgs()[0] : 
'/Library/Frameworks/Mono.framework/Versions/2.10.2/lib/mono/4.0/csharp.exe'
Environment.GetCommandLineArgs()[1] : '-lib:$PWD'
Environment.GetCommandLineArgs()[2] : './timefromseconds'
Environment.GetCommandLineArgs()[3] : '-d:foo'
Environment.GetCommandLineArgs()[4] : '-d:bar'
Current Time (Total Seconds): 1309369313.60381
Current Local Time: 6/29/2011 9:41:53 AM
Current UTC Time: 6/29/2011 4:41:53 PM
 

And here's my new script code:


#!/usr/bin/env csharp -lib:$PWD

int counter = 0; foreach (string arg in 
Environment.GetCommandLineArgs()) 
Console.WriteLine("Environment.GetCommandLineArgs()[{0}] : '{1}'", 
counter++, arg);

// Initialize the "epoch"
// The "epoch" is 12:00:00 AM UTC, January 1, 1970
DateTime epoch = new DateTime().AddYears(1969).AddHours(-8);

TimeSpan nowDiff = (DateTime.Now - epoch);
System.Console.WriteLine("Current Time (Total Seconds): " + 
nowDiff.TotalSeconds);
System.Console.WriteLine("Current Local Time: " + 
epoch.AddSeconds(nowDiff.TotalSeconds).ToString());
System.Console.WriteLine("Current UTC Time: " + 
epoch.AddSeconds(nowDiff.TotalSeconds).ToUniversalTime().ToString());








On Tue, 28 Jun 2011 20:19:32 +0200, Alex wrote:
> Hi,
> 
> Arguments should be stored in Environment.CommandLine.
> 
> Regards,
> Alex
> 
> On Tue, Jun 28, 2011 at 7:58 PM, Ian Norton
> <Ian.Norton-Badrul at thales-esecurity.com> wrote:
>> Try looking at Mono.Options (aka NDesk.Options)
>> 
>> That works quite well for me.
>> 
>> Oh.. You don't have a main in repl that gets argv...
>> 
>> Are the command line arguments stored in the environment somewhere?
>> 
>> Ian
>> 
>> On 28 Jun 2011, at 18:59, "Steve Lessard" <s_lessard at yahoo.com> wrote:
>> 
>>> Hi,
>>> 
>>> I'm trying to write a shell script in C# using the csharp REPL.  The
>>> script below works fine as is on OS X, but I would like to pass an
>>> argument into this script. I haven't been able to find any docs on how
>>> to do this and all of my experiments have turned up nothing. Is
>>> there a
>>> way to pass an argument into the csharp REPL?
>>> 
>>> I'm running Mono version 2.10.2 with Mono C# compiler version 4.0.0.0
>>> 
>>> -SteveL
>>> 
>>> 
>>> #!/usr/bin/env csharp -lib:$PWD
>>> 
>>> // Initialize the "epoch"
>>> // The "epoch" is 12:00:00 AM UTC, January 1, 1970
>>> DateTime epoch = new DateTime().AddYears(1969).AddHours(-8);
>>> 
>>> TimeSpan nowDiff = (DateTime.Now - epoch);
>>> System.Console.WriteLine("Current Time (Total Seconds): " +
>>> nowDiff.TotalSeconds);
>>> System.Console.WriteLine("Current Local Time: " +
>>> epoch.AddSeconds(nowDiff.TotalSeconds).ToString());
>>> System.Console.WriteLine("Current UTC Time: " +
>>> epoch.AddSeconds(nowDiff.TotalSeconds).ToUniversalTime().ToString());
>>> 
>>> _______________________________________________
>>> Mono-list maillist  -  Mono-list at lists.ximian.com
>>> http://lists.ximian.com/mailman/listinfo/mono-list
>> _______________________________________________
>> Mono-list maillist  -  Mono-list at lists.ximian.com
>> http://lists.ximian.com/mailman/listinfo/mono-list
>> 


More information about the Mono-list mailing list