Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 2ba207a

Browse files
committed
allow binary files for file upload via --binary flag
1 parent 10b30c9 commit 2ba207a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

cli4/cli4.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ def do_it(args):
272272
raw = False
273273
dump = False
274274
dump_from_web = False
275+
binary_file = False
275276
profile = None
276277
method = 'GET'
277278

@@ -281,19 +282,21 @@ def do_it(args):
281282
+ '[-r|--raw] '
282283
+ '[-d|--dump] '
283284
+ '[-a|--api] '
285+
+ '[-b|--binary] '
284286
+ '[-p|--profile profile-name] '
285287
+ '[--get|--patch|--post|--put|--delete] '
286288
+ '[item=value|item=@filename|@filename ...] '
287289
+ '/command ...')
288290

289291
try:
290292
opts, args = getopt.getopt(args,
291-
'Vhvqjyrdap:GPOUD',
293+
'Vhvqjyrdabp:GPOUD',
292294
[
293295
'version',
294296
'help', 'verbose', 'quiet', 'json', 'yaml', 'ndjson',
295297
'raw',
296298
'dump', 'api',
299+
'binary',
297300
'profile=',
298301
'get', 'patch', 'post', 'put', 'delete'
299302
])
@@ -326,6 +329,8 @@ def do_it(args):
326329
dump = True
327330
elif opt in ('-a', '--api'):
328331
dump_from_web = True
332+
elif opt in ('-b', '--binary'):
333+
binary_file = True
329334
elif opt in ('-G', '--get'):
330335
method = 'GET'
331336
elif opt in ('-P', '--patch'):
@@ -363,10 +368,17 @@ def do_it(args):
363368
sys.exit('cli4: %s - raw file upload only with PUT or POST' % (filename))
364369
try:
365370
if filename == '-':
366-
content = sys.stdin.read()
371+
if binary_file:
372+
content = sys.stdin.buffer.read()
373+
else:
374+
content = sys.stdin.read()
367375
else:
368-
with open(filename, 'r') as f:
369-
content = f.read()
376+
if binary_file:
377+
with open(filename, 'rb') as f:
378+
content = f.read()
379+
else:
380+
with open(filename, 'r') as f:
381+
content = f.read()
370382
except IOError:
371383
sys.exit('cli4: %s - file open failure' % (filename))
372384
continue

0 commit comments

Comments
 (0)