comp.lang.ada
 help / color / mirror / Atom feed
* problem with command line
@ 2007-01-11  0:08 [Taz]
  2007-01-11  0:47 ` Frank J. Lhota
  2007-01-11  0:51 ` Jeffrey Creem
  0 siblings, 2 replies; 5+ messages in thread
From: [Taz] @ 2007-01-11  0:08 UTC (permalink / raw)


hi,
I have a problem with the command line ...
I declare two variables like in the code below.
If the call to my file don't have parameter, I get a error ...
How can I solve this?
...

procedure Test is

Nome1 : String := Argument (1);

Nome2 : String := Argument (2);

...





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: problem with command line
  2007-01-11  0:08 problem with command line [Taz]
@ 2007-01-11  0:47 ` Frank J. Lhota
  2007-01-11  9:02   ` Brian May
  2007-01-11  0:51 ` Jeffrey Creem
  1 sibling, 1 reply; 5+ messages in thread
From: Frank J. Lhota @ 2007-01-11  0:47 UTC (permalink / raw)


"[Taz]" <tazdany@hotmail.com> wrote in message 
news:45a58068$0$3828$5402220f@news.sunrise.ch...
> hi,
> I have a problem with the command line ...
> I declare two variables like in the code below.
> If the call to my file don't have parameter, I get a error ...
> How can I solve this?
> ...
>
> procedure Test is
>
> Nome1 : String := Argument (1);
>
> Nome2 : String := Argument (2);
>
> ...

The package Ada.Command_Line includes the function Argument_Count that 
returns the number of command line arguments. You can use it to make sure 
that there is an argument 2 before calling Argument(2).

I would probably code your application something like this:

with Ada.Command_Line;
use Ada.Command_Line;
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;

procedure Test is

   Nome1 : Unbounded_String;
   Nome2 : Unbounded_String;

begin

   if Argument_Count >= 1 then
      Nome1 := To_Unbounded_String(Argument(1));
   else
      Nome1 := To_Unbounded_String("Default_Nome1");
   end if;

   if Argument_Count >= 2 then
      Nome2 := To_Unbounded_String(Argument(2));
   else
      Nome2 := To_Unbounded_String("Default_Nome2");
   end if;

   ...

end Test;

-- 
"All things extant in this world,
Gods of Heaven, gods of Earth,
Let everything be as it should be;
Thus shall it be!"
- Magical chant from "Magical Shopping Arcade Abenobashi"

"Drizzle, Drazzle, Drozzle, Drome,
Time for this one to come home!"
- Mr. Wizard from "Tooter Turtle"





^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: problem with command line
  2007-01-11  0:08 problem with command line [Taz]
  2007-01-11  0:47 ` Frank J. Lhota
@ 2007-01-11  0:51 ` Jeffrey Creem
  1 sibling, 0 replies; 5+ messages in thread
From: Jeffrey Creem @ 2007-01-11  0:51 UTC (permalink / raw)


[Taz] wrote:
> hi,
> I have a problem with the command line ...
> I declare two variables like in the code below.
> If the call to my file don't have parameter, I get a error ...
> How can I solve this?
> ...
> 
> procedure Test is
> 
> Nome1 : String := Argument (1);
> 
> Nome2 : String := Argument (2);
> 
> ...
> 
> 

Check to Ada.Command_Line.Argument_Count first to be sure that you 
actually have parameters 1 and 2. This will of course require that you 
not declare strings and assign them during declaration but then this 
requires you to use access types for the strings or accessor functions 
or some other method.

For example, you could create a function like

function Get_Argument_And_Null_If_Not_Present(I : Positive) return String is

begin
   if Ada.Command_Line.Argument_Count >= I then
     return Ada.Command_Line.Argument(I);
   else
     return "";
   end if;
end Get_Argument_And_Null_If_Not_Present;

And use that to fill in your strings.

It very much depends on what you are trying to accomplish.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: problem with command line
  2007-01-11  0:47 ` Frank J. Lhota
@ 2007-01-11  9:02   ` Brian May
  2007-01-11 10:39     ` Martin Krischik
  0 siblings, 1 reply; 5+ messages in thread
From: Brian May @ 2007-01-11  9:02 UTC (permalink / raw)


>>>>> "Frank" == Frank J Lhota <FrankLho.NOSPAM@rcn.com> writes:

    Frank> I would probably code your application something like this:

    Frank> if Argument_Count >= 1 then
    Frank> Nome1 := To_Unbounded_String(Argument(1));
    Frank> else
    Frank> Nome1 := To_Unbounded_String("Default_Nome1");
    Frank> end if;

    Frank> if Argument_Count >= 2 then
    Frank> Nome2 := To_Unbounded_String(Argument(2));
    Frank> else
    Frank> Nome2 := To_Unbounded_String("Default_Nome2");
    Frank> end if;

    Frank> ...

Probably less usable (actually I can't see any applications that don't
have a messed up command line interface), but I think the following
would also work:

=== cut ===
with Ada.Command_Line;
use Ada.Command_Line;
with Ada.Strings.Unbounded;
use Ada.Strings.Unbounded;

procedure Test is

   Nome1 : Unbounded_String;
   Nome2 : Unbounded_String;

begin

   if Argument_Count >= 2 then
      declare
        Nome1 : String := Argument (1);
        Nome2 : String := Argument (2);
      begin
      ...
      end

   elsif Argument_Count = 1
      declare
        Nome1 : String := Argument (1);
      begin
      ...
      end
   else
      ...
   end if;

   ...

end Test;
=== cut ===

Disclaimer: I program in different languages, lets hope I got the
syntax right ;-)
-- 
Brian May <bam@snoopy.apana.org.au>



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: problem with command line
  2007-01-11  9:02   ` Brian May
@ 2007-01-11 10:39     ` Martin Krischik
  0 siblings, 0 replies; 5+ messages in thread
From: Martin Krischik @ 2007-01-11 10:39 UTC (permalink / raw)


Brian May schrieb:

> Probably less usable (actually I can't see any applications that don't
> have a messed up command line interface), but I think the following
> would also work:

Not true, my applications have a perfect GNU compatible commandline 
interface:

http://adacl.sourceforge.net/html/______Scripts________Include__adacl-command_line-getopt__ads.htm

Martin



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-01-11 10:39 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-11  0:08 problem with command line [Taz]
2007-01-11  0:47 ` Frank J. Lhota
2007-01-11  9:02   ` Brian May
2007-01-11 10:39     ` Martin Krischik
2007-01-11  0:51 ` Jeffrey Creem

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox