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] #11059

Closed
p00f opened this issue Nov 15, 2023 · 1 comment · Fixed by #11203
Closed

Expose argv[0] #11059

p00f opened this issue Nov 15, 2023 · 1 comment · Fixed by #11203
Labels
enhancement New feature or request scripting Related to writing scripts
Milestone

Comments

@p00f
Copy link
Contributor

p00f commented Nov 15, 2023

Related problem

I would like argv[0] ie the name the script was invoked with, exposed in scripts

Describe the solution you'd like

argv[0] is available as the path argument here:

The information is lost here:

let file_path = canonicalize_with(&path, cwd).unwrap_or_else(|e| {
let working_set = StateWorkingSet::new(engine_state);
report_error(
&working_set,
&ShellError::FileNotFoundCustom(
format!("Could not access file '{}': {:?}", path, e.to_string()),
Span::unknown(),
),
);
std::process::exit(1);
});

stack.add_env_var(
"CURRENT_FILE".to_string(),
Value::string(file_path.to_string_lossy(), Span::unknown()),
);

Describe alternatives you've considered

$"./($env.CURRENT_FILE | path relative-to (pwd))", but this is not necessarily the same thing as argv[0] (an obvious exception is windows)

Additional context and details

No response

@p00f p00f added enhancement New feature or request needs-triage An issue that hasn't had any proper look labels Nov 15, 2023
@p00f
Copy link
Contributor Author

p00f commented Nov 15, 2023

This should also be used when reporting usage errors:

> ./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.

In the last line, ./tools/build.nu should be used instead of main

@sholderbach sholderbach added the scripting Related to writing scripts label Nov 15, 2023
@amtoine amtoine removed the needs-triage An issue that hasn't had any proper look label Nov 15, 2023
fdncred pushed a commit that referenced this issue Dec 2, 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
<!-- 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.
-->
@hustcer hustcer added this to the v0.88.0 milestone Dec 3, 2023
hardfau1t pushed a commit to hardfau1t/nushell that referenced this issue 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 issue 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
enhancement New feature or request scripting Related to writing scripts
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants