TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

How do you get current directory for command line tools?

Forums > macOS

For fun, I'm writing a Swift command line tool for macOS. I want to pass in an argument like "foo.txt", which would be a text file in the current directory. I'm unclear on how the app will know what directory the shell is currently in. How do you pass the current directory to the command line apps implicity so the user does not have to specify them?

3      

I think FileManager.currentDirectoryPath is what you are looking for.

4      

Thank you, David. I tested it and you are correct!

I disregarded currentDirectoryPath initially because the documentation says: "The path to the program’s current directory."

If I'd read the full docs, I'd have seen the warning: "This property reports the current working directory for the current process, not just the receiver."

Which is exactly what I wanted, thanks again!

3      

Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn More

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.