question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to retreive command line arguments

See original GitHub issue

I’m working on an application right now where I’d like the user to be able to launch the application via a terminal, but also be able to specify a default file to load. e.g. myApp image.png I’m wondering if there is an easy way for my application to retrieve the command line argument that were passed into it at this part:

BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);    // `args` comes from `Program.cs`'s `Main()`

E.g. In my App’s code, I’d like to be able to retreive those args and parse them to find an initial file:

        public override void OnFrameworkInitializationCompleted()
        {
            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                desktop.MainWindow = new MainWindow
                {
                    DataContext = new MainWindowViewModel(),
                };
                // Get those `args` somewhere around here and then pass them into a function in the `DataContext`

            }

            base.OnFrameworkInitializationCompleted();
        }

I saw that there was this entry on the wiki: https://github.com/AvaloniaUI/Avalonia/wiki/Application-lifetimes#manual-lifetime-management

AFAICT, it seems like the only way to pass those command line args off to my app’s code. Unless there is another way?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
out4blood88commented, Nov 11, 2022

@Nikhilsp I put this in App.axaml.cs:

public override void OnFrameworkInitializationCompleted()
{
    var desktopLifetime = ApplicationLifetime as IClassicDesktopStyleApplicationLifetime;
    desktopLifetime!.Startup += (sender, args) =>
    {
        // Do stuff with args
        // Create your window
    };
}
0reactions
FaridGadcommented, Jul 14, 2023

@out4blood88 Your code works but the designer previewer of the .axaml files stops working. It just remains stuck on “The designer is loading”.

grafik

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I find out command line arguments of a running ...
Right click on any column and select "Show Columns" and then check "Command Line Arguments" option > Click OK. Then select the process...
Read more >
windows - How to get list of arguments?
%0 - the command used to call the batch file (could be foo , ..\foo , c:\bats\foo.bat , etc.) %1 is the first...
Read more >
Command Line Arguments in C/C++
To pass command-line arguments, we typically define main() with two arguments: the first argument is the number of command-line arguments and ...
Read more >
How to Get the Command Line Args Passed to a Running ...
1. Introduction · 2. Finding the PID of a Process · 3. Using the ps Command to Get Arguments Using PID · 4....
Read more >
Environment.GetCommandLineArgs Method (System)
Returns a string array containing the command-line arguments for the ... To obtain the command line as a single string, use the CommandLine...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found