# Lack of API mutating args at std::process::Command

**URL:** <https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908>\
**Category:** libs\
**Created:** [June 22, 2021, 4:34pm UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908 "2021-06-22T16:34:56Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![cehteh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cehteh/32/8289_2.png) [@cehteh](https://internals.rust-lang.org/u/cehteh)\
**Post date:** [June 24, 2021, 2:16pm UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/21 "2021-06-24T14:16:46Z")

</div>

My saying, still clearing the argument list for recreating it would be the most minimal functionality needed and is lacking currently. I would like to have an API that allows the mutation of an _existing_ argument as shown above, because that is very common.

About the env-mess I'd wait and see what the solution on [Why should std::env::var panic?](https://internals.rust-lang.org/t/why-should-std-var-panic/14847) will look like finally and then follow that route. Note that I put a comment there about that env keys may get their distinct type, dunno how feasible that is (to me it boils down to "will rust want to make things correct even when it introduces a breaking change or staying bug by bug compatible").

---

<div class="post-metadata">

**Author:** ![zackw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/zackw/32/2071_2.png) [@zackw](https://internals.rust-lang.org/u/zackw)\
**Post date:** [June 24, 2021, 6:14pm UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/22 "2021-06-24T18:14:08Z")

</div>

> [@cehteh](#):
>
> For args it is very common that one wants to update an argument at a certain position. I am thinking this really deserves to have an API, not for efficiency (which is irrelevant when we talking about spawning processes), but for ergonomic reasons with something like:
> 
> ```rust
> let mut command = Command::new("dosomething").args(["FILE"]);
> somefiles.iter().for_each(|file| command.arg_set(1, file)?.spawn());
> 
> ```

I have written many programs that needed to do things like this (not in Rust) and I would like to explicitly vote for the addition of `.arg_set(index, string)`. It can easily be quite a bit hairier than @cehteh's example, e.g. from a Python script I wrote for work

```python
proc = subprocess.Popen([BRO, "-b", "-C", BRO_SCRIPT,
                         "-r", input_file, "-w", "-"],
                        stdout=subprocess.PIPE, ...)

```

where the argument that changes for each file to be processed is third-to-last, and has to be there because of the way the process being run parses its arguments. In Python it's no big deal to create a scratch argument array for every invocation, but in Rust it would be quite a bit longer I think.

---

<div class="post-metadata">

**Author:** ![cehteh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cehteh/32/8289_2.png) [@cehteh](https://internals.rust-lang.org/u/cehteh)\
**Post date:** [July 5, 2021, 1:50pm UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/23 "2021-07-05T13:50:24Z")

</div>

What would be the way to progress here (only the 'arg' part)? Opening an issue, Writing an RFC or sending a PR?

I haven't done such yet and don't know the process. I can offer to do the coding for Linux/Unix, but don't know Windows and other platforms. Would be nice if someone cold lend me a hand.

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [July 6, 2021, 12:10am UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/24 "2021-07-06T00:10:01Z")

</div>

According to [the guide](https://forge.rust-lang.org/libs/maintaining-std.html#when-is-an-rfc-needed):

> New unstable features don't need an RFC before they can be merged. If the feature is small, and the design space is straightforward, stabilizing it usually only requires the feature to go through FCP. Sometimes however, you may ask for an RFC before stabilizing.

I'm not certain but it sounds like you could make a PR and there would be an FCP (where most of the libs team review the proposal).

Though if you're not sure you can ping `@rust-lang/libs` on a github issue or [ask on zulip](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs).

---

<div class="post-metadata">

**Author:** ![yaahc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/yaahc/32/6606_2.png) [@yaahc](https://internals.rust-lang.org/u/yaahc)\
**Post date:** [July 6, 2021, 12:19am UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/25 "2021-07-06T00:19:45Z")

</div>

As long as it doesn't change the stable API it wouldn't even need an FCP. Just make the PR and an associated tracking issue for it's eventual stabilization.

---

<div class="post-metadata">

**Author:** ![cehteh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cehteh/32/8289_2.png) [@cehteh](https://internals.rust-lang.org/u/cehteh)\
**Post date:** [July 22, 2021, 6:20pm UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/26 "2021-07-22T18:20:43Z")

</div>

I opened a related issue, will start on implementing this soon.

> <https://github.com/rust-lang/rust/issues/87379>
>
> Feature gate: \`#!\[feature(mutate\_command\_args)\]\`
> 
> This is a tracking issue for… adding an API to mutate the arguments of std::process::Command.
> Discussed earlier in https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908
> 
> \### Public API
> 
> \`\`\`rust
> // std::process::Command
> 
> pub fn args\_clear(&mut self) -\> &mut Command;
> 
> pub fn arg\_set\<S\>(&mut self, index: usize, value: S) -\> &mut Command
> where
> S: AsRef\<OsStr\>;
> 
> // maybe for convenience (same as .args\_clear().args(new\_args)):
> pub fn args\_new\<I, S\>(&mut self, args: I) -\> &mut Command
> where
> I: IntoIterator\<Item = S\>,
> S: AsRef\<OsStr\>;
> \`\`\`
> 
> \`arg\_set()\` may panic when the index is out of range, only existing elements get replaced.
> 
> \### Steps / History
> 
> \- \[\] Implementation: #...
> \- \[\] Final comment period (FCP)
> \- \[\] Stabilization PR
> 
> \<!--
> Once the feature has gone through a few release cycles and there are no
> unresolved questions left, the feature might be ready for stabilization.
> 
> If this feature didn't go through the RFC process, a final comment period
> (FCP) is always needed before stabilization. This works as follows:
> 
> A library API team member can kick off the stabilization process, at which point
> the rfcbot will ask all the team members to verify they agree with
> stabilization. Once enough members agree and there are no concerns, the final
> comment period begins: this issue will be marked as such and will be listed
> in the next This Week in Rust newsletter. If no blocking concerns are raised in
> that period of 10 days, a stabilzation PR can be opened by anyone.
> \--\>
> 
> \### Unresolved Questions
> 
> \<!--
> Include any open questions that need to be answered before the feature can be
> stabilised. If multiple (unrelated) big questions come up, it can be a good idea
> to open a separate issue for each, to make it easier to keep track of the
> discussions.
> 
> It's useful to link any relevant discussions and conclusions (whether on GitHub,
> Zulip, or the internals forum) here.
> \--\>
> 
> \- None yet.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [October 20, 2021, 6:21pm UTC](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908/27 "2021-10-20T18:21:26Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

[Previous page](https://internals.rust-lang.org/t/lack-of-api-mutating-args-at-std-command/14908.md?page=1)
