Как разделить строку с shell-командой на отдельные аргументы в виде списка?
Если сделать просто сплит по пробелу то получим то что надо, кроме случаев со вставками текста с пробелами. Например так:
>>> '-arg "I Am Groot"'.split(' ')
['-arg', '"I', 'Am', 'Groot"']
Чтобы учитывать текст в кавычках как единый аргумент можно воспользоваться функцией shlex.split()
Кто читает мой канал давно, уже в курсе.
А что делать, если нужно обратное действие? Объединить аргументы из списка в строку и при этом добавить кавычки в аргумент с пробелами.
Конечно, если вы используете subprocess то он сам всё разрулит. Но если вам нужна именно команда одной строкой, то можно воспользоваться готовой функцией в том же subprocess.
>>> from subprocess import list2cmdline
>>> list2cmdline(['-arg', 'I Am Groot'])
'-arg "I Am Groot"'
Он также позаботится об экранировании уже имеющихся кавычек
>>> list2cmdline(['-arg', 'I Am "Groot"'])
'-arg "I Am \"Groot\""'
А вот так он может "схлопнуть" в команду JSON
>>> list2cmdline(['--json', json.dumps({'key': 'value'})])
'--json "{\"key\": \"value\"}"'
_______________
Возможно кто-то спросит, а зачем соединять аргументы в строку если subprocess сам это сделает а os.system не наш путь?
Мне как-то потребовалось отправлять команду на удалённое выполнение и в API поддерживалось указание команды только строкой. Так что всякое бывает)
#libs#basic
🏆 GuPay Team Cup – 2025 FIFA Club World Cup Edition is LIVE! ⚽️
Choose your team. Power it with every transaction.
From the pitch to the blockchain — this is where Web3 meets real-world passion. ⚽️
🗓 June 9 – July 15, 2025
Support 1 of 32 legendary clubs from the official 2025 FIFA Club World Cup, use GuPay, and help your squad rise to the top!
🎯 How to join:
1️⃣Pick your favorite club by filling out the registration form: link
2️⃣ Make transactions using GuPay throughout the tournament
3️⃣ Each transaction boosts your team’s total score on the leaderboard
🎁 $5,000 Prize Pool
The entire reward will be shared among users who joined the team with the highest total transaction volume via GuPay during the campaign.
📌 Fill the form now to lock in your team choice: link
📊 Team standings and leaderboard will be updated regularly — stay tuned!
🌐 From sport to spending, culture to crypto — Gumart is bringing Web3 to real life, one goal at a time.
#GuPayTeamCup#GuGiveaway
🏆 GuPay Team Cup – FIFA Vibes, $5,000 Prize Pool! 📈
Something big is coming to GuPay... and it’s dressed in jerseys, flags, and fire.✨
⚽️ 32 teams - inspired by the world of FIFA
💥 Fierce competition, full of national pride
💰A massive $5,000 prize pool waiting at the finish line
Choose your team. Make your moves.
Support your side and help them rise to the top.
Can you guess what’s coming? Stay tuned 👀
#GuPayTeamCup#GuGiveaway
🛍 GuPay Team Cup – Midway Update: Who’s Leading the Race?
It’s been nearly three weeks since the GuPay Team Cup kicked off - and the competition is getting intense! With thousands of Gumart residents rallying behind their favorite FIFA clubs, the blockchain scoreboard is anything but predictable. ⚽️💸
Here’s the current Top 4 (by GuPay transaction volume):
🥇 Real Madrid – Still leading with unstoppable momentum
🥈Chelsea – Not far behind, showing strong team spirit – A surprise rise from Africa
🏅Al Hilal SFC – Consistency and strategy paying off
🏅Manchester City – Still showing its strength
🎉Remember: $5,000 prize pool will be distributed to users who joined the team with the highest total GuPay transaction volume by the end of the tournament.
🚀 Don’t forget to:
✅ Register your team: link
✅ Transact with GuPay before July 15
✅ Watch the leaderboard change in real-time
From football fields to financial freedom — this is Web3 in action, where fans aren’t just cheering, they’re shaping the outcome. ✋
🏁 2 weeks left. One winner. Will it be your team?
#GuPayTeamCup#GuMinigame#FIFAClubWorldCup2025
🏆 GuPay Team Cup – The Race Is On! ⚽️
Just 3 days in, and over 2,000 Gumart residents have jumped into the GuPay Team Cup – 2025 FIFA Club World Cup Edition!🚀
🔥 The leaderboard is heating up:
1️⃣ Real Madrid is leading the charge
2️⃣ Chelsea right behind
3️⃣ Manchester City chasing hard!
Want your team to rise? 💥
👉 Every GuPay transaction boosts your team’s score and brings you closer to the $5,000 prize pool!
📍Choose your team now and start transacting: link
#GuPayTeamCup#GuGiveaway#FIFAClubWorldCup
‼10,000+ TRANSACTIONS IN 20 DAYS 🤑
GuPay Team Cup – FIFA Club World Cup 2025 is going global - and it’s powered by BNB Chain ⚡️
From Real Madrid to Man City, 32 legendary football clubs are battling it out. But this time, it's not just on the field — it's on-chain. 🏟
📣 Gumart is rewriting what mass adoption looks like:
10,000+ real-world transactions made through GuPay across hotels, stores, and events — all in less than 3 weeks.⬆️
🚀 Built on BNB Chain's trusted infrastructure, Gumart is leading the charge in making crypto usable, visible, and impactful in everyday life.
➖Want to join?
Pick your team and make transactions with GuPay. Help your squad rise to the top.
🌐 Be where culture, payments, and crypto collide.
#GuPayTeamCup#Gumart#BNBChain