Command line parameters and environment variables/es

From Lazarus wiki
Revision as of 21:50, 27 January 2009 by Iskraelectrica (talk | contribs)
Jump to navigationJump to search

English (en) español (es) suomi (fi) français (fr) русский (ru)

Parámetros de la línea de órdenes y variables de entorno

Introducción

   Al arrancar un programa el usuario puede poner en la línea de órdenes parámetros y variables de entorno de la configuración. Por ejemplo, el compilador de FreePascal obtiene la mayoría de sus parámetros por medio de las opciones de la línea de orden:

 fpc -Fudirectory -gh unidad1.pas

La esencia de Parámetros de la línea de órdenes

    Un programa Pascal accede a los parámetros por medio de ParamStr y ParamCount. ParamStr(0) contiene la ruta y nombre del programa mismo. ParamStr(1) es el primer parámetro. ParamCount es el número de parámetros.

<delphi> program Project1; {$mode objfpc}{$H+}

var
  i: Integer;
begin
  writeln('Programa: ',ParamStr(0));
  for i:=1 to ParamCount do
    writeln('Parámetro  ',i,': ',ParamStr(i));
end. </delphi>

    Por ejemplo:

 $ /tmp/proyecto1 -a
  Programa: /tmp/proyecto1
  Parámetro 1: -a 

Parámetros de línea de órdenes cómodos

    Un buen programa debe dar un mensaje de ayuda cuando es invocado con los parámetros incorrectos y debe seguir una forma consistente de leer los parámetros. La unidad custapp que viene con FPC proporciona la clase TCustomApplication, que proporciona las funciones que fácilmente comprueba y lee parámetros. Por supuesto se puede tener acceso a los parámetros directamente vía ParamStr y ParamCount.

    Every LCL application uses this automatically. The Application object is a TCustomApplication.

    If you want to write a non LCL program, then create in lazarus a new project of type 'Console Application'. This will create a project1.lpr with some nice goodies, that almost all programs need. Go to the DoRun method.

Check for a parameter

    With TCustomApplication you can access parameters by name. For example your program should print a help text when the user gave the common help parameter -h. The -h is a short option. The long form is the --help. To test whether the user called the program with -h or --help you can use <delphi> if HasOption('h','help') then begin

  WriteHelp;
  Halt;
end; </delphi>

    Note: In an LCL form you must prepend Application. in front of HasOption. For example:

<delphi> if Application.HasOption('h','help') then begin

  WriteHelp;
  Halt;
end; </delphi>

<delphi> // If you only want to support the short option use:

if HasOption('h',) then ...
// If you only want to support the long option use:
if HasOption('help') then ... </delphi>

Read the parameter value

    Each parameter can be given a value. For example:

 project1 -f filename

    or with the long form:

 project1 --file=filename

<delphi> writeln('f=',GetOptionValue('f','file')); </delphi>

    Note: if you get the error message Option at position 1 needs an argument : f. then you forgot to add the option in the CheckOptions call.

Checking parameters for validity

    Command line parameters are free text, so the user can easily do typing errors. Checking the syntax of the parameters is therefore mandatory. You can use the CheckOptions method for this:

    You can define, what parameters are allowed, which ones ones need a parameter and in case of a syntax error you can get an error message plus the options that were wrong to print helpful and detailed errors.

    Examples: <delphi> ErrorMsg:=CheckOptions('hf:','help file:');</delphi>

    This allows passing short options -f value and -h. It allows passing long options --help or --file=filename. It does not allow --help with a value, nor --file without a value.