Passing PHP Function Arguments from CLI vs URL

You can do this:

function foo_command( $args ) {
    WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo_command' );

And put it in a plugin, then run:

wp foo "hello world"

And it will print out hello world.

Just because the built in subcommands don’t do what you need them to do, doesn’t mean you can’t add your own. WP CLI will take care of bootstrapping WordPress and loading things properly ( and it avoids the security issues of standalone endpoint PHP files such as the one in your question )

However, I suspect the solution to your actual problem is this:

wp post list --name="hello-world" --fields="id"

And for the REST API:

https://example.com/wp-json/wp/v2/posts/slug?="hello-world"