-#tesh file
-if(!$ARGV[$nb_arg]){die "tesh.pl <options> <teshfile.tesh>\n";}
-print "[Tesh/INFO] load file : $ARGV[$nb_arg]\n";
-my($file)=$ARGV[$nb_arg];
-open SH_LIGNE, $file or die "[Tesh/CRITICAL] Unable to open $file. $!\n";
+
+#options
+sub cd_cmd {
+ my $directory=$_[1];
+ if (-e $directory) {
+ chdir("$directory");
+ print "[Tesh/INFO] change directory to $directory\n";
+ } else {
+ die "[Tesh/CRITICAL] Directory not found : \"$directory\"\n";
+ }
+}
+
+sub setenv_cmd {
+ if ($_[1] =~ /^(.*)=(.*)$/) {
+ my($var,$ctn)=($1,$2);
+ $ENV{$var} = $ctn;
+ print "[Tesh/INFO] setenv $var=$ctn\n";
+ } else {
+ die "[Tesh/CRITICAL] Malformed argument to setenv: expected 'name=value' but got '$_[1]'\n";
+ }
+}
+
+my $tesh_file;
+sub get_options {
+ # remove the tesh file from the ARGV used
+ my @ARGV = @_;
+ $tesh_file = pop @ARGV;
+
+ # temporary arrays for GetOption
+ my @verbose = ();
+ my @cfg;
+
+ my %opt = (
+ "help" => 0,
+ "debug" => 0,
+ "verbose" => 0
+ );
+
+ Getopt::Long::config('bundling', 'no_getopt_compat', 'no_auto_abbrev');
+
+ GetOptions(
+ 'help|h' => \$opt{'help'},
+
+ 'verbose|v' => \@verbose,
+ 'debug|d' => \$opt{"debug"},
+
+ 'cd=s' => \&cd_cmd,
+ 'setenv=s' => \&setenv_cmd,
+ 'cfg=s' => \@cfg,
+ );
+
+ $opt{'verbose'} = scalar @verbose;
+ foreach (@cfg) {
+ $opt{'cfg'} .= " --cfg=$_";
+ }
+ return %opt;
+}
+
+my %opts = get_options(@ARGV);
+
+# parse tesh file
+open SH_LIGNE, $tesh_file or die "[Tesh/CRITICAL] Unable to open $tesh_file: $!\n";