• Accessing The Command Line

    From Lawrence D'Oliveiro@21:1/5 to All on Thu Jul 4 00:08:56 2024
    with Ada.Command_Line;
    with Ada.Text_IO;

    procedure Echo is

    package cli renames Ada.Command_Line;
    package tio renames Ada.Text_IO;
    package int_io is new tio.Integer_IO(Num => Integer);

    begin
    tio.put("my name: ");
    tio.put(cli.Command_name);
    tio.Put_Line("");
    tio.Put("nr args: ");
    int_io.Put(cli.Argument_Count, width => 1);
    tio.Put_Line("");
    for i in 1 .. cli.Argument_Count loop
    tio.put("[");
    int_io.put(i, width => 1);
    tio.put("]: ");
    tio.put(cli.argument(i));
    tio.put_line("");
    end loop;
    end Echo;

    ----

    Comments:

    Ada, like Python, offers the convenience of being able to specify
    local “nicknames” for imported packages, to save some typing.

    Having become used to the convenience of printf-style formatting in C
    and other languages that have adopted it (including Lisp and Python),
    I don’t miss the tedium of having to format and output one item at a
    time. Though I recognize that there is no way to do printf style in a
    type-safe fashion, short of going to a fully-dynamic language.

    Being able to access the POSIX command line via some globally-defined
    entity instead of arguments to a “mainline” procedure is something
    that just about every decent language offers. C is notably absent from
    this list.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Paul Rubin on Thu Jul 4 01:50:59 2024
    On Wed, 03 Jul 2024 18:16:01 -0700, Paul Rubin wrote:

    C++ does that polymorphism stuff in iostream so you'd write
    std::cout << "my name: " << command_line << ...

    I know. The disadvantage of the C++ scheme is you cannot easily reorder
    items as necessary to fit the grammar of localized messages. That, I
    think, is why lots of other languages (including Python and Lisp) have
    copied the printf scheme, yet none have seen fit to copy the C++ scheme.

    printf for some people is one of the motivations for dependent types.

    I wonder how you would express such a thing in an Ada-friendly fashion?

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Lawrence D'Oliveiro on Wed Jul 3 18:16:01 2024
    Lawrence D'Oliveiro <ldo@nz.invalid> writes:
    Though I recognize that there is no way to do printf style in a
    type-safe fashion, short of going to a fully-dynamic language.

    C++ does that polymorphism stuff in iostream so you'd write
    std::cout << "my name: " << command_line << ...

    Haskell does something sort of similar with additional machinery.

    printf for some people is one of the motivations for dependent types.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeffrey R.Carter@21:1/5 to Lawrence D'Oliveiro on Thu Jul 4 13:27:05 2024
    On 2024-07-04 02:08, Lawrence D'Oliveiro wrote:

    Remember that you can concatenate strings:

    tio.put("my name: ");
    tio.put(cli.Command_name);
    tio.Put_Line("");

    Tio.Put_Line (Item => "my name: " & Cli.Command_Name);

    Image functions thus allow similar simplifications. 'Image is one such function,
    if you can accept the initial space for non-negative values:

    tio.Put("nr args: ");
    int_io.Put(cli.Argument_Count, width => 1);
    tio.Put_Line("");

    Tio.Put_Line (Item => "nr args:" & Cli.Argument_Count'Image);

    For simple cases you can roll your own:

    function Image (Value : in Integer) return String is
    Raw : constant String := Value'Image;
    begin -- Image
    return Raw ( (if Value < 0 then 1 else 2) .. Raw'Last);
    end Image;

    tio.put("[");
    int_io.put(i, width => 1);
    tio.put("]: ");
    tio.put(cli.argument(i));
    tio.put_line("");

    Tio.Put_Line (Item => '[' & Image (I) & "]: " & Image (Cli.Argument (I) ) );

    For more complex uses, you can use something like PragmARC.Images[.Image] (https://github.com/jrcarter/PragmARC).

    You probably should review the definition of Ada.Text_IO (http://www.ada-auth.org/standards/aarm12_w_tc1/html/AA-A-10.html), especially for procedure New_Line.

    --
    Jeff Carter
    "Write clearly--don't sacrifice clarity for 'efficiency.'"
    Elements of Programming Style
    186

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Lawrence D'Oliveiro on Thu Jul 4 15:01:05 2024
    On 2024-07-04 02:08, Lawrence D'Oliveiro wrote:
    with Ada.Command_Line;
    with Ada.Text_IO;
    [...]

    A general advice processing strings, any strings: messages, commands,
    payload etc.

    Always read a complete string into a fixed size buffer (safety). Never
    use streams. Process the whole string consequently. Never tokenize.
    Never copy anything. Ada has slices.

    The same is true for the output. Build a complete substring in a buffer. Consequently. Flush the complete substring to the output.

    Do not use Unbounded_String.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Lawrence D'Oliveiro on Thu Jul 4 18:15:54 2024
    On 2024-07-04 03:50, Lawrence D'Oliveiro wrote:
    On Wed, 03 Jul 2024 18:16:01 -0700, Paul Rubin wrote:

    printf for some people is one of the motivations for dependent types.

    I wonder how you would express such a thing in an Ada-friendly fashion?

    For example:

    http://www.dmitry-kazakov.de/ada/strings_edit.htm

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Lawrence D'Oliveiro on Thu Jul 4 20:42:00 2024
    Lawrence D'Oliveiro <ldo@nz.invalid> writes:

    ... Though I recognize that there is no way to do printf style in a
    type-safe fashion, short of going to a fully-dynamic language.

    No so. Haskell has Text.Printf.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Rubin@21:1/5 to Ben Bacarisse on Thu Jul 4 15:06:00 2024
    Ben Bacarisse <ben@bsb.me.uk> writes:
    ... Though I recognize that there is no way to do printf style in a
    type-safe fashion, short of going to a fully-dynamic language.
    No so. Haskell has Text.Printf.

    Text.Printf is not fully type safe. printf "%d\n" "foo"
    throws an "bad formatting character" exception, really amounting to
    a runtime type exception.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Jeffrey R.Carter on Thu Jul 4 23:54:49 2024
    On Thu, 4 Jul 2024 13:27:05 +0200, Jeffrey R.Carter wrote:

    On 2024-07-04 02:08, Lawrence D'Oliveiro wrote:

    Remember that you can concatenate strings:

    tio.put("my name: ");
    tio.put(cli.Command_name);
    tio.Put_Line("");

    Tio.Put_Line (Item => "my name: " & Cli.Command_Name);

    I’m sure I can, but I’m not sure what the point is. Let Ada collect the pieces in its own buffers. That saves copying steps.

    For more complex uses, you can use something like
    PragmARC.Images[.Image] (https://github.com/jrcarter/PragmARC).

    I don’t really feel the need to resort to third-party libraries just to do simple I/O.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From J-P. Rosen@21:1/5 to All on Fri Jul 5 10:58:00 2024
    Le 05/07/2024 à 01:54, Lawrence D'Oliveiro a écrit :
    On Thu, 4 Jul 2024 13:27:05 +0200, Jeffrey R.Carter wrote:

    On 2024-07-04 02:08, Lawrence D'Oliveiro wrote:

    Remember that you can concatenate strings:

    tio.put("my name: ");
    tio.put(cli.Command_name);
    tio.Put_Line("");
    Tio.Put_Line (Item => "my name: " & Cli.Command_Name);
    I’m sure I can, but I’m not sure what the point is. Let Ada collect the pieces in its own buffers. That saves copying steps.

    Agreed. I don't understand why people dislike printing piece by piece.
    In the old FORTRAN, you could write only line by line, but this time is
    long gone...
    With the various Put procedures, you have individual formatting options
    that you don't have otherwise. Moreover, there is a nice property that
    few people noticed: if you have an algorithm writing data to a file,
    with loops and so on, you can keep the exact same structure replacing
    every Put with the matching Get, and you will read your data correctly.
    This feature goes away as soon as you have a 'Image.

    --
    J-P. Rosen
    Adalog
    2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
    https://www.adalog.fr https://www.adacontrol.fr

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)