8 url-parts - Extract specified parts from URLs given in input stream
12 echo <URL> | url-parts <PART> [<PART> [<PART> [...]]]
17 fragment, hostname, netloc, password, path, port, query, scheme, username,
18 and query.I<NAME> for the query parameter I<NAME>,
19 and query.I<NAME>.I<N> for I<N>th element of the array parameter I<NAME>.
21 Run C<url-parts --help> for the definitive list of URL part names
22 supported by the python urlparse module installed on your system.
28 from __future__
import print_function
35 if '--help' in sys
.argv
:
36 sample
= urlparse
.urlsplit('')
37 print("Usage: echo URL | url-parts [PART-1 [PART-2 [...]]]")
38 print("PART-N is one of:", ' '.join([attr
for attr
in dir(sample
)
39 if not attr
.startswith('_') and not hasattr(getattr(sample
, attr
), '__call__')]))
40 print("or 'query.PARAM' to access individual query string parameters,")
41 print("or 'query.PARAM.N' where N is an integer to access array-like parameters.")
45 line
= sys
.stdin
.readline()
49 url
= urlparse
.urlsplit(line
)
51 for param_name
, param_values
in urlparse
.parse_qs(url
.query
).iteritems():
52 field_name
= 'query.%s' % param_name
53 if len(param_values
) == 1:
54 setattr(url
, field_name
, param_values
[0])
55 for index
, param_value
in enumerate(param_values
):
56 setattr(url
, '%s.%d' % (field_name
, index
), param_value
)
57 for arg
in sys
.argv
[1:]:
59 value
= getattr(url
, arg
)
63 print(delimiter
, end
='')