Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose argv[0] as $env.PROCESS_PATH #11203

Merged
merged 2 commits into from Dec 2, 2023
Merged

expose argv[0] as $env.PROCESS_PATH #11203

merged 2 commits into from Dec 2, 2023

Conversation

p00f
Copy link
Contributor

@p00f p00f commented Dec 1, 2023

closes #11059

Description

I'm not sure what the consensus was after discussing this in discord, so I'm creating a PR as suggested

User-Facing Changes

$env.PROCESS_PATH can now be used in scripts to get the name the script was invoked with

#!/usr/bin/env nu

def main [] {
    printf $"I was called as ($env.PROCESS_PATH)"
}
/tmp/workspaces/0PMKZ: ./foo
I was called as ./foo
/tmp/workspaces/0PMKZ: nu foo
I was called as foo

Tests + Formatting

After Submitting

@p00f
Copy link
Contributor Author

p00f commented Dec 1, 2023

@fdncred @sophiajt

@p00f
Copy link
Contributor Author

p00f commented Dec 1, 2023

even if we don't reach a consensus on this, i'd like to at least fix this: #11059 (comment), so pointers would be appreciated

@fdncred
Copy link
Collaborator

fdncred commented Dec 1, 2023

ORIGINAL_CMDLINE infers the command and all the parameters, is that how it works?

@p00f
Copy link
Contributor Author

p00f commented Dec 2, 2023

No, it's already there (all the way back from nu's main - which uses std::env::args())

This is why ORIGINAL_CMDLINE is a bad name

@fdncred edited^^^

@fdncred
Copy link
Collaborator

fdncred commented Dec 2, 2023

ok, I don't really like ORIGINAL_CMDLINE so I'm throwing out some others.

  • PROGRAM_NAME
  • EXE_NAME
  • EXECUTABLE
  • PROGRAM_FILENAME
  • LAUNCH_CMD
  • PROCESS_PATH
  • ARGV0

I'm kind of leaning towards PROCESS_PATH, any takers?

@p00f
Copy link
Contributor Author

p00f commented Dec 2, 2023

Same

@sholderbach
Copy link
Member

When the name bikeshedding is done let's update the title to make the commit better discoverable.

@p00f p00f changed the title expose argv[0] expose argv[0] as $env.PROCESS_PATH Dec 2, 2023
@p00f
Copy link
Contributor Author

p00f commented Dec 2, 2023

@sholderbach does the pr title look good?

@sholderbach
Copy link
Member

Thanks! 👍

@p00f
Copy link
Contributor Author

p00f commented Dec 2, 2023

also, when a script has a def main,

> ./tools/build.nu
Error: nu::parser::missing_positional

  × Missing required positional argument.
   ╭─[<commandline>:1:1]
 1 │ main
   ·     ▲
   ·     ╰── missing src
   ╰────
  help: Usage: main <src> <dest> . Use `--help` for more information.

the error message (last line) should also use this process path instead of main.

Where is the code for this?

@fdncred
Copy link
Collaborator

fdncred commented Dec 2, 2023

Let's handle the "main" part in another PR. My suggestion would be to search for "main" in the code and MissingPositional to see where they're used.

Seems like this is what's being called. You may be able to track it down from here.

#[error("Missing required positional argument.")]
#[diagnostic(
code(nu::parser::missing_positional),
help("Usage: {2}. Use `--help` for more information.")
)]
MissingPositional(String, #[label("missing {0}")] Span, String),

@fdncred fdncred merged commit 58d002d into nushell:main Dec 2, 2023
19 checks passed
@amtoine
Copy link
Member

amtoine commented Dec 2, 2023

@p00f
i've updated the PR description (a bit too late), can you confirm it's correct?

@p00f
Copy link
Contributor Author

p00f commented Dec 2, 2023

@amtoine fixed it

hardfau1t pushed a commit to hardfau1t/nushell that referenced this pull request Dec 14, 2023
closes nushell#11059 

# Description
I'm not sure what the consensus was after discussing this in discord, so
I'm creating a PR as suggested

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
TBD
# Tests + Formatting
TBD
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
TBD
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
dmatos2012 pushed a commit to dmatos2012/nushell that referenced this pull request Feb 20, 2024
closes nushell#11059 

# Description
I'm not sure what the consensus was after discussing this in discord, so
I'm creating a PR as suggested

# User-Facing Changes
<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->
TBD
# Tests + Formatting
TBD
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to
check that you're using the standard code style
- `cargo test --workspace` to check that all tests pass (on Windows make
sure to [enable developer
mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging))
- `cargo run -- -c "use std testing; testing run-tests --path
crates/nu-std"` to run the tests for the standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
TBD
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose argv[0]
4 participants